From 3014759df80eef81e518adbfe61035b0495c9072 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 31 Aug 2023 21:16:53 -0400 Subject: [PATCH] Factor out per-controller storage into test helper. (#28999) --- .../CHIPTests/MTRPerControllerStorageTests.m | 95 +++---------------- .../TestHelpers/MTRTestPerControllerStorage.h | 43 +++++++++ .../TestHelpers/MTRTestPerControllerStorage.m | 85 +++++++++++++++++ .../Matter.xcodeproj/project.pbxproj | 6 ++ 4 files changed, 148 insertions(+), 81 deletions(-) create mode 100644 src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.h create mode 100644 src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.m diff --git a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m index fdf03eab12ba36..894eb5f3893464 100644 --- a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m +++ b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m @@ -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; @@ -69,73 +69,6 @@ - (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSEr @end -@interface MTRPerControllerStorageTestsStorageDelegate : NSObject -@property (nonatomic, readonly) NSMutableDictionary * 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)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)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 - (instancetype)initWithRootCertificate:(MTRCertificateDERBytes)rootCertificate intermediateCertificate:(MTRCertificateDERBytes _Nullable)intermediateCertificate @@ -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: @@ -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 @@ -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 { @@ -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 @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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. @@ -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 @@ -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 diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.h b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.h new file mode 100644 index 00000000000000..b3052a9929a1af --- /dev/null +++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.h @@ -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 +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface MTRTestPerControllerStorage : NSObject + +- (instancetype)initWithControllerID:(NSUUID *)controllerID; + +@property (nonatomic, readonly) NSUUID * controllerID; + +- (nullable id)controller:(MTRDeviceController *)controller + valueForKey:(NSString *)key + securityLevel:(MTRStorageSecurityLevel)securityLevel + sharingType:(MTRStorageSharingType)sharingType; +- (BOOL)controller:(MTRDeviceController *)controller + storeValue:(id)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 diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.m b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.m new file mode 100644 index 00000000000000..1898bd14832cf9 --- /dev/null +++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.m @@ -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 + +#import "MTRTestPerControllerStorage.h" + +@interface MTRTestPerControllerStorage () +@property (nonatomic, readonly) NSMutableDictionary * storage; +@end + +@implementation MTRTestPerControllerStorage + +- (instancetype)initWithControllerID:(NSUUID *)controllerID +{ + if (!(self = [super init])) { + return nil; + } + + _storage = [[NSMutableDictionary alloc] init]; + _controllerID = controllerID; + return self; +} + +- (nullable id)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)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 diff --git a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj index 1520c2a9d12082..edc68cd47eb1c4 100644 --- a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj +++ b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj @@ -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, ); }; }; @@ -486,6 +487,8 @@ 517BF3EE282B62B800A8B7DB /* MTRCertificates.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRCertificates.h; sourceTree = ""; }; 517BF3EF282B62B800A8B7DB /* MTRCertificates.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRCertificates.mm; sourceTree = ""; }; 517BF3F2282B62CB00A8B7DB /* MTRCertificateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRCertificateTests.m; sourceTree = ""; }; + 518D3F812AA132DC008E0007 /* MTRTestPerControllerStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRTestPerControllerStorage.m; sourceTree = ""; }; + 518D3F822AA132DC008E0007 /* MTRTestPerControllerStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRTestPerControllerStorage.h; sourceTree = ""; }; 519498312A25581C00B3BABE /* MTRSetupPayloadSerializerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRSetupPayloadSerializerTests.m; sourceTree = ""; }; 51A2F1312A00402A00F03298 /* MTRDataValueParserTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRDataValueParserTests.m; sourceTree = ""; }; 51B22C1D2740CB0A008D5055 /* MTRStructsObjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRStructsObjc.h; sourceTree = ""; }; @@ -968,6 +971,8 @@ 51742B4829CB5F45009974FE /* MTRTestResetCommissioneeHelper.h */, 51742B4929CB5FC0009974FE /* MTRTestResetCommissioneeHelper.m */, 51C984612A61CE2A00B0AD9A /* MTRFabricInfoChecker.h */, + 518D3F822AA132DC008E0007 /* MTRTestPerControllerStorage.h */, + 518D3F812AA132DC008E0007 /* MTRTestPerControllerStorage.m */, 51C984602A61CE2A00B0AD9A /* MTRFabricInfoChecker.m */, ); path = TestHelpers; @@ -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 */,