From 1549006359be9e4a43d51cfb1407fb359121fd8c Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Thu, 23 May 2024 16:16:01 -0700 Subject: [PATCH] Fixing usage of NSUserDefaults (#33576) * Fixing NSUserDefaults * Fixing key * Fixing name * Moving this back for now * Updating header * Restyled by clang-format * Fixing build --------- Co-authored-by: Restyled.io --- .../Framework/CHIP/MTRDeviceController.mm | 9 +++---- .../MTRDeviceControllerLocalTestStorage.mm | 24 +++++++++---------- .../Framework/CHIP/MTRDevice_Internal.h | 8 +++++++ .../CHIPTests/MTRPerControllerStorageTests.m | 13 ++++------ src/platform/Darwin/UserDefaults.mm | 3 +-- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index a66e4f186e1f02..1bc4e6e039221e 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm @@ -145,9 +145,6 @@ - (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParamete return [MTRDeviceControllerFactory.sharedInstance initializeController:self withParameters:controllerParameters error:error]; } -static NSString * const kLocalTestUserDefaultDomain = @"org.csa-iot.matter.darwintest"; -static NSString * const kLocalTestUserDefaultSubscriptionPoolSizeOverrideKey = @"subscriptionPoolSizeOverride"; - - (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory queue:(dispatch_queue_t)queue storageDelegate:(id _Nullable)storageDelegate @@ -256,9 +253,9 @@ - (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory } // Provide a way to test different subscription pool sizes without code change - NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; - if ([defaults objectForKey:kLocalTestUserDefaultSubscriptionPoolSizeOverrideKey]) { - NSInteger subscriptionPoolSizeOverride = [defaults integerForKey:kLocalTestUserDefaultSubscriptionPoolSizeOverrideKey]; + NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; + if ([defaults objectForKey:kDefaultSubscriptionPoolSizeOverrideKey]) { + NSInteger subscriptionPoolSizeOverride = [defaults integerForKey:kDefaultSubscriptionPoolSizeOverrideKey]; if (subscriptionPoolSizeOverride < 1) { concurrentSubscriptionPoolSize = 1; } else { diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerLocalTestStorage.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerLocalTestStorage.mm index e1d039fd58053b..fa4a0e456f5626 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerLocalTestStorage.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerLocalTestStorage.mm @@ -16,27 +16,25 @@ */ #import "MTRDeviceControllerLocalTestStorage.h" +#import "MTRDevice_Internal.h" #import "MTRLogging_Internal.h" -static NSString * const kLocalTestUserDefaultDomain = @"org.csa-iot.matter.darwintest"; -static NSString * const kLocalTestUserDefaultEnabledKey = @"enableTestStorage"; - @implementation MTRDeviceControllerLocalTestStorage { id _passThroughStorage; } + (BOOL)localTestStorageEnabled { - NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; - return [defaults boolForKey:kLocalTestUserDefaultEnabledKey]; + NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; + return [defaults boolForKey:kTestStorageUserDefaultEnabledKey]; } + (void)setLocalTestStorageEnabled:(BOOL)localTestStorageEnabled { - NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; - [defaults setBool:localTestStorageEnabled forKey:kLocalTestUserDefaultEnabledKey]; + NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; + [defaults setBool:localTestStorageEnabled forKey:kTestStorageUserDefaultEnabledKey]; MTR_LOG("MTRDeviceControllerLocalTestStorage setLocalTestStorageEnabled %d", localTestStorageEnabled); - BOOL storedLocalTestStorageEnabled = [defaults boolForKey:kLocalTestUserDefaultEnabledKey]; + BOOL storedLocalTestStorageEnabled = [defaults boolForKey:kTestStorageUserDefaultEnabledKey]; if (storedLocalTestStorageEnabled != localTestStorageEnabled) { MTR_LOG_ERROR("MTRDeviceControllerLocalTestStorage setLocalTestStorageEnabled %d failed", localTestStorageEnabled); } @@ -58,7 +56,7 @@ - (instancetype)initWithPassThroughStorage:(id> *)valuesForController:(MTRDeviceController *)controller securityLevel:(MTRStorageSecurityLevel)securityLevel sharingType:(MTRStorageSharingType)sharingType { if (sharingType == MTRStorageSharingTypeNotShared) { - NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; + NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; return [defaults dictionaryRepresentation]; } else { if (_passThroughStorage && [_passThroughStorage respondsToSelector:@selector(valuesForController:securityLevel:sharingType:)]) { @@ -136,7 +134,7 @@ - (BOOL)controller:(MTRDeviceController *)controller - (BOOL)controller:(MTRDeviceController *)controller storeValues:(NSDictionary> *)values securityLevel:(MTRStorageSecurityLevel)securityLevel sharingType:(MTRStorageSharingType)sharingType { if (sharingType == MTRStorageSharingTypeNotShared) { - NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; + NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; BOOL success = YES; for (NSString * key in values) { NSError * error = nil; diff --git a/src/darwin/Framework/CHIP/MTRDevice_Internal.h b/src/darwin/Framework/CHIP/MTRDevice_Internal.h index f5383f301fe4af..c479627221fda0 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDevice_Internal.h @@ -120,4 +120,12 @@ MTR_TESTABLE // Returns min or max, if it is below or above, respectively. NSNumber * MTRClampedNumber(NSNumber * aNumber, NSNumber * min, NSNumber * max); +#pragma mark - Constants + +static NSString * const kDefaultSubscriptionPoolSizeOverrideKey = @"subscriptionPoolSizeOverride"; +static NSString * const kTestStorageUserDefaultEnabledKey = @"enableTestStorage"; + +// Declared inside platform, but noting here for reference +// static NSString * const kSRPTimeoutInMsecsUserDefaultKey = @"SRPTimeoutInMSecsOverride"; + NS_ASSUME_NONNULL_END diff --git a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m index 4a87fd66a7d88d..1cd4b2139ce412 100644 --- a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m +++ b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m @@ -2051,9 +2051,6 @@ - (void)testSetMRPParametersWithRunningController [controller shutdown]; } -static NSString * const kLocalTestUserDefaultDomain = @"org.csa-iot.matter.darwintest"; -static NSString * const kLocalTestUserDefaultSubscriptionPoolSizeOverrideKey = @"subscriptionPoolSizeOverride"; - // TODO: This might also want to go in a separate test file, with some shared setup for commissioning devices per test - (void)doTestSubscriptionPoolWithSize:(NSInteger)subscriptionPoolSize { @@ -2075,11 +2072,11 @@ - (void)doTestSubscriptionPoolWithSize:(NSInteger)subscriptionPoolSize NSError * error; - NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; - NSNumber * subscriptionPoolSizeOverrideOriginalValue = [defaults objectForKey:kLocalTestUserDefaultSubscriptionPoolSizeOverrideKey]; + NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; + NSNumber * subscriptionPoolSizeOverrideOriginalValue = [defaults objectForKey:kDefaultSubscriptionPoolSizeOverrideKey]; // Test DeviceController with a Subscription pool - [defaults setInteger:subscriptionPoolSize forKey:kLocalTestUserDefaultSubscriptionPoolSizeOverrideKey]; + [defaults setInteger:subscriptionPoolSize forKey:kDefaultSubscriptionPoolSizeOverrideKey]; MTRPerControllerStorageTestsCertificateIssuer * certificateIssuer; MTRDeviceController * controller = [self startControllerWithRootKeys:rootKeys @@ -2182,9 +2179,9 @@ - (void)doTestSubscriptionPoolWithSize:(NSInteger)subscriptionPoolSize XCTAssertFalse([controller isRunning]); if (subscriptionPoolSizeOverrideOriginalValue) { - [defaults setInteger:subscriptionPoolSizeOverrideOriginalValue.integerValue forKey:kLocalTestUserDefaultSubscriptionPoolSizeOverrideKey]; + [defaults setInteger:subscriptionPoolSizeOverrideOriginalValue.integerValue forKey:kDefaultSubscriptionPoolSizeOverrideKey]; } else { - [defaults removeObjectForKey:kLocalTestUserDefaultSubscriptionPoolSizeOverrideKey]; + [defaults removeObjectForKey:kDefaultSubscriptionPoolSizeOverrideKey]; } } diff --git a/src/platform/Darwin/UserDefaults.mm b/src/platform/Darwin/UserDefaults.mm index d24b0a4ee20f37..e28c1fa90fc0de 100644 --- a/src/platform/Darwin/UserDefaults.mm +++ b/src/platform/Darwin/UserDefaults.mm @@ -21,7 +21,6 @@ #import -static NSString * const kUserDefaultDomain = @"org.csa-iot.matter.darwin"; static NSString * const kSRPTimeoutInMsecsUserDefaultKey = @"SRPTimeoutInMSecsOverride"; namespace chip { @@ -29,7 +28,7 @@ std::optional GetUserDefaultDnssdSRPTimeoutInMSecs() { - NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kUserDefaultDomain]; + NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; NSInteger srpTimeoutValue = [defaults integerForKey:kSRPTimeoutInMsecsUserDefaultKey]; if (CanCastTo(srpTimeoutValue)) { uint16_t timeoutinMsecs = static_cast(srpTimeoutValue);