Skip to content

Commit

Permalink
Factor out per-controller storage into test helper. (#28999)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Mar 15, 2024
1 parent 55b8559 commit 3014759
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 81 deletions.
95 changes: 14 additions & 81 deletions src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#import "MTRErrorTestUtils.h"
#import "MTRFabricInfoChecker.h"
#import "MTRTestKeys.h"
#import "MTRTestPerControllerStorage.h"
#import "MTRTestResetCommissioneeHelper.h"
#import "MTRTestStorage.h"

static const uint16_t kPairingTimeoutInSeconds = 10;
static const uint16_t kTimeoutInSeconds = 3;
Expand Down Expand Up @@ -69,73 +69,6 @@ - (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSEr

@end

@interface MTRPerControllerStorageTestsStorageDelegate : NSObject <MTRDeviceControllerStorageDelegate>
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSData *> * storage;
@property (nonatomic, readonly) NSUUID * controllerID;
@end

@implementation MTRPerControllerStorageTestsStorageDelegate

- (instancetype)initWithControllerID:(NSUUID *)controllerID
{
if (!(self = [super init])) {
return nil;
}

_storage = [[NSMutableDictionary alloc] init];
_controllerID = controllerID;
return self;
}

- (nullable id<NSSecureCoding>)controller:(MTRDeviceController *)controller
valueForKey:(NSString *)key
securityLevel:(MTRStorageSecurityLevel)securityLevel
sharingType:(MTRStorageSharingType)sharingType
{
XCTAssertEqualObjects(_controllerID, controller.uniqueIdentifier);

__auto_type * data = self.storage[key];
if (data == nil) {
return data;
}

NSError * error;
id value = [NSKeyedUnarchiver unarchivedObjectOfClasses:MTRDeviceControllerStorageClasses() fromData:data error:&error];
XCTAssertNil(error);
XCTAssertNotNil(data);

return value;
}

- (BOOL)controller:(MTRDeviceController *)controller
storeValue:(id<NSSecureCoding>)value
forKey:(NSString *)key
securityLevel:(MTRStorageSecurityLevel)securityLevel
sharingType:(MTRStorageSharingType)sharingType
{
XCTAssertEqualObjects(_controllerID, controller.uniqueIdentifier);

NSError * error;
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:value requiringSecureCoding:YES error:&error];
XCTAssertNil(error);
XCTAssertNotNil(data);

self.storage[key] = data;
return YES;
}

- (BOOL)controller:(MTRDeviceController *)controller
removeValueForKey:(NSString *)key
securityLevel:(MTRStorageSecurityLevel)securityLevel
sharingType:(MTRStorageSharingType)sharingType
{
XCTAssertEqualObjects(_controllerID, controller.uniqueIdentifier);
self.storage[key] = nil;
return YES;
}

@end

@interface MTRPerControllerStorageTestsCertificateIssuer : NSObject <MTROperationalCertificateIssuer>
- (instancetype)initWithRootCertificate:(MTRCertificateDERBytes)rootCertificate
intermediateCertificate:(MTRCertificateDERBytes _Nullable)intermediateCertificate
Expand Down Expand Up @@ -319,7 +252,7 @@ - (nullable MTRDeviceController *)startControllerWithRootKeys:(MTRTestKeys *)roo
operationalKeys:(MTRTestKeys *)operationalKeys
fabricID:(NSNumber *)fabricID
nodeID:(NSNumber *)nodeID
storage:(MTRPerControllerStorageTestsStorageDelegate *)storage
storage:(MTRTestPerControllerStorage *)storage
caseAuthenticatedTags:(nullable NSSet *)caseAuthenticatedTags
error:(NSError * __autoreleasing *)error
certificateIssuer:
Expand Down Expand Up @@ -376,7 +309,7 @@ - (nullable MTRDeviceController *)startControllerWithRootKeys:(MTRTestKeys *)roo
operationalKeys:(MTRTestKeys *)operationalKeys
fabricID:(NSNumber *)fabricID
nodeID:(NSNumber *)nodeID
storage:(MTRPerControllerStorageTestsStorageDelegate *)storage
storage:(MTRTestPerControllerStorage *)storage
error:(NSError * __autoreleasing *)error
certificateIssuer:
(MTRPerControllerStorageTestsCertificateIssuer * __autoreleasing *)certificateIssuer
Expand All @@ -395,7 +328,7 @@ - (nullable MTRDeviceController *)startControllerWithRootKeys:(MTRTestKeys *)roo
operationalKeys:(MTRTestKeys *)operationalKeys
fabricID:(NSNumber *)fabricID
nodeID:(NSNumber *)nodeID
storage:(MTRPerControllerStorageTestsStorageDelegate *)storage
storage:(MTRTestPerControllerStorage *)storage
caseAuthenticatedTags:(nullable NSSet *)caseAuthenticatedTags
error:(NSError * __autoreleasing *)error
{
Expand All @@ -413,7 +346,7 @@ - (nullable MTRDeviceController *)startControllerWithRootKeys:(MTRTestKeys *)roo
operationalKeys:(MTRTestKeys *)operationalKeys
fabricID:(NSNumber *)fabricID
nodeID:(NSNumber *)nodeID
storage:(MTRPerControllerStorageTestsStorageDelegate *)storage
storage:(MTRTestPerControllerStorage *)storage
error:(NSError * __autoreleasing *)error
{
return [self startControllerWithRootKeys:rootKeys
Expand All @@ -436,7 +369,7 @@ - (void)test001_BasicControllerStartup
__auto_type * operationalKeys = [[MTRTestKeys alloc] init];
XCTAssertNotNil(operationalKeys);

__auto_type * storageDelegate = [[MTRPerControllerStorageTestsStorageDelegate alloc] initWithControllerID:[NSUUID UUID]];
__auto_type * storageDelegate = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]];

NSNumber * nodeID = @(123);
NSNumber * fabricID = @(456);
Expand Down Expand Up @@ -481,7 +414,7 @@ - (void)test002_TryStartingTwoControllersWithSameNodeID
__auto_type * operationalKeys = [[MTRTestKeys alloc] init];
XCTAssertNotNil(operationalKeys);

__auto_type * storageDelegate = [[MTRPerControllerStorageTestsStorageDelegate alloc] initWithControllerID:[NSUUID UUID]];
__auto_type * storageDelegate = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]];

NSNumber * nodeID = @(123);
NSNumber * fabricID = @(456);
Expand Down Expand Up @@ -526,7 +459,7 @@ - (void)test003_TestTwoControllersSameUUID
XCTAssertNotNil(operationalKeys);
XCTAssertEqual(operationalKeys.signatureCount, 0);

__auto_type * storageDelegate = [[MTRPerControllerStorageTestsStorageDelegate alloc] initWithControllerID:[NSUUID UUID]];
__auto_type * storageDelegate = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]];

NSNumber * fabricID = @(456);

Expand Down Expand Up @@ -578,7 +511,7 @@ - (void)test004_TestBasicSessionResumption
XCTAssertNotNil(operationalKeys);
XCTAssertEqual(operationalKeys.signatureCount, 0);

__auto_type * storageDelegate = [[MTRPerControllerStorageTestsStorageDelegate alloc] initWithControllerID:[NSUUID UUID]];
__auto_type * storageDelegate = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]];

NSNumber * nodeID = @(123);
NSNumber * fabricID = @(456);
Expand Down Expand Up @@ -675,7 +608,7 @@ - (void)test005_TestSessionResumptionDataClearingNodeIDChanged
XCTAssertNotNil(operationalKeys);
XCTAssertEqual(operationalKeys.signatureCount, 0);

__auto_type * storageDelegate = [[MTRPerControllerStorageTestsStorageDelegate alloc] initWithControllerID:[NSUUID UUID]];
__auto_type * storageDelegate = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]];

NSNumber * nodeID1 = @(123);
NSNumber * nodeID2 = @(246);
Expand Down Expand Up @@ -778,7 +711,7 @@ - (void)test006_TestSessionResumptionDataClearingCATsChanged
XCTAssertNotNil(operationalKeys);
XCTAssertEqual(operationalKeys.signatureCount, 0);

__auto_type * storageDelegate = [[MTRPerControllerStorageTestsStorageDelegate alloc] initWithControllerID:[NSUUID UUID]];
__auto_type * storageDelegate = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]];

NSNumber * nodeID = @(123);
NSNumber * fabricID = @(456);
Expand Down Expand Up @@ -887,7 +820,7 @@ - (void)test007_TestMultipleControllers
NSNumber * fabricID1 = @(1);
NSNumber * fabricID2 = @(2);

__auto_type * storageDelegate1 = [[MTRPerControllerStorageTestsStorageDelegate alloc] initWithControllerID:[NSUUID UUID]];
__auto_type * storageDelegate1 = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]];

// Start several controllers that have distinct identities but share some
// node/fabric IDs.
Expand All @@ -904,7 +837,7 @@ - (void)test007_TestMultipleControllers
XCTAssertNotNil(controller1);
XCTAssertTrue([controller1 isRunning]);

__auto_type * storageDelegate2 = [[MTRPerControllerStorageTestsStorageDelegate alloc] initWithControllerID:[NSUUID UUID]];
__auto_type * storageDelegate2 = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]];
MTRDeviceController * controller2 = [self startControllerWithRootKeys:rootKeys
operationalKeys:operationalKeys
fabricID:fabricID1
Expand All @@ -915,7 +848,7 @@ - (void)test007_TestMultipleControllers
XCTAssertNotNil(controller2);
XCTAssertTrue([controller2 isRunning]);

__auto_type * storageDelegate3 = [[MTRPerControllerStorageTestsStorageDelegate alloc] initWithControllerID:[NSUUID UUID]];
__auto_type * storageDelegate3 = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]];
MTRDeviceController * controller3 = [self startControllerWithRootKeys:rootKeys
operationalKeys:operationalKeys
fabricID:fabricID2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>
#import <Matter/Matter.h>

NS_ASSUME_NONNULL_BEGIN

@interface MTRTestPerControllerStorage : NSObject <MTRDeviceControllerStorageDelegate>

- (instancetype)initWithControllerID:(NSUUID *)controllerID;

@property (nonatomic, readonly) NSUUID * controllerID;

- (nullable id<NSSecureCoding>)controller:(MTRDeviceController *)controller
valueForKey:(NSString *)key
securityLevel:(MTRStorageSecurityLevel)securityLevel
sharingType:(MTRStorageSharingType)sharingType;
- (BOOL)controller:(MTRDeviceController *)controller
storeValue:(id<NSSecureCoding>)value
forKey:(NSString *)key
securityLevel:(MTRStorageSecurityLevel)securityLevel
sharingType:(MTRStorageSharingType)sharingType;
- (BOOL)controller:(MTRDeviceController *)controller
removeValueForKey:(NSString *)key
securityLevel:(MTRStorageSecurityLevel)securityLevel
sharingType:(MTRStorageSharingType)sharingType;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <XCTest/XCTest.h>

#import "MTRTestPerControllerStorage.h"

@interface MTRTestPerControllerStorage ()
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSData *> * storage;
@end

@implementation MTRTestPerControllerStorage

- (instancetype)initWithControllerID:(NSUUID *)controllerID
{
if (!(self = [super init])) {
return nil;
}

_storage = [[NSMutableDictionary alloc] init];
_controllerID = controllerID;
return self;
}

- (nullable id<NSSecureCoding>)controller:(MTRDeviceController *)controller
valueForKey:(NSString *)key
securityLevel:(MTRStorageSecurityLevel)securityLevel
sharingType:(MTRStorageSharingType)sharingType
{
XCTAssertEqualObjects(_controllerID, controller.uniqueIdentifier);

__auto_type * data = self.storage[key];
if (data == nil) {
return data;
}

NSError * error;
id value = [NSKeyedUnarchiver unarchivedObjectOfClasses:MTRDeviceControllerStorageClasses() fromData:data error:&error];
XCTAssertNil(error);
XCTAssertNotNil(data);

return value;
}

- (BOOL)controller:(MTRDeviceController *)controller
storeValue:(id<NSSecureCoding>)value
forKey:(NSString *)key
securityLevel:(MTRStorageSecurityLevel)securityLevel
sharingType:(MTRStorageSharingType)sharingType
{
XCTAssertEqualObjects(_controllerID, controller.uniqueIdentifier);

NSError * error;
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:value requiringSecureCoding:YES error:&error];
XCTAssertNil(error);
XCTAssertNotNil(data);

self.storage[key] = data;
return YES;
}

- (BOOL)controller:(MTRDeviceController *)controller
removeValueForKey:(NSString *)key
securityLevel:(MTRStorageSecurityLevel)securityLevel
sharingType:(MTRStorageSharingType)sharingType
{
XCTAssertEqualObjects(_controllerID, controller.uniqueIdentifier);
self.storage[key] = nil;
return YES;
}

@end
6 changes: 6 additions & 0 deletions src/darwin/Framework/Matter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
517BF3F0282B62B800A8B7DB /* MTRCertificates.h in Headers */ = {isa = PBXBuildFile; fileRef = 517BF3EE282B62B800A8B7DB /* MTRCertificates.h */; settings = {ATTRIBUTES = (Public, ); }; };
517BF3F1282B62B800A8B7DB /* MTRCertificates.mm in Sources */ = {isa = PBXBuildFile; fileRef = 517BF3EF282B62B800A8B7DB /* MTRCertificates.mm */; };
517BF3F3282B62CB00A8B7DB /* MTRCertificateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 517BF3F2282B62CB00A8B7DB /* MTRCertificateTests.m */; };
518D3F832AA132DC008E0007 /* MTRTestPerControllerStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = 518D3F812AA132DC008E0007 /* MTRTestPerControllerStorage.m */; };
519498322A25581C00B3BABE /* MTRSetupPayloadSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 519498312A25581C00B3BABE /* MTRSetupPayloadSerializerTests.m */; };
51A2F1322A00402A00F03298 /* MTRDataValueParserTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 51A2F1312A00402A00F03298 /* MTRDataValueParserTests.m */; };
51B22C1E2740CB0A008D5055 /* MTRStructsObjc.h in Headers */ = {isa = PBXBuildFile; fileRef = 51B22C1D2740CB0A008D5055 /* MTRStructsObjc.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -486,6 +487,8 @@
517BF3EE282B62B800A8B7DB /* MTRCertificates.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRCertificates.h; sourceTree = "<group>"; };
517BF3EF282B62B800A8B7DB /* MTRCertificates.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRCertificates.mm; sourceTree = "<group>"; };
517BF3F2282B62CB00A8B7DB /* MTRCertificateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRCertificateTests.m; sourceTree = "<group>"; };
518D3F812AA132DC008E0007 /* MTRTestPerControllerStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRTestPerControllerStorage.m; sourceTree = "<group>"; };
518D3F822AA132DC008E0007 /* MTRTestPerControllerStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRTestPerControllerStorage.h; sourceTree = "<group>"; };
519498312A25581C00B3BABE /* MTRSetupPayloadSerializerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRSetupPayloadSerializerTests.m; sourceTree = "<group>"; };
51A2F1312A00402A00F03298 /* MTRDataValueParserTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRDataValueParserTests.m; sourceTree = "<group>"; };
51B22C1D2740CB0A008D5055 /* MTRStructsObjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRStructsObjc.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -968,6 +971,8 @@
51742B4829CB5F45009974FE /* MTRTestResetCommissioneeHelper.h */,
51742B4929CB5FC0009974FE /* MTRTestResetCommissioneeHelper.m */,
51C984612A61CE2A00B0AD9A /* MTRFabricInfoChecker.h */,
518D3F822AA132DC008E0007 /* MTRTestPerControllerStorage.h */,
518D3F812AA132DC008E0007 /* MTRTestPerControllerStorage.m */,
51C984602A61CE2A00B0AD9A /* MTRFabricInfoChecker.m */,
);
path = TestHelpers;
Expand Down Expand Up @@ -1581,6 +1586,7 @@
1E5801C328941C050033A199 /* MTRTestOTAProvider.m in Sources */,
5A6FEC9D27B5E48900F25F42 /* MTRXPCProtocolTests.m in Sources */,
1EE0805E2A44875E008A03C2 /* MTRCommissionableBrowserTests.m in Sources */,
518D3F832AA132DC008E0007 /* MTRTestPerControllerStorage.m in Sources */,
51339B1F2A0DA64D00C798C1 /* MTRCertificateValidityTests.m in Sources */,
5173A47929C0E82300F67F48 /* MTRFabricInfoTests.m in Sources */,
5143851E2A65885500EDC8E6 /* MTRSwiftPairingTests.swift in Sources */,
Expand Down

0 comments on commit 3014759

Please sign in to comment.