Skip to content

Commit

Permalink
Fixing usage of NSUserDefaults (#33576)
Browse files Browse the repository at this point in the history
* Fixing NSUserDefaults

* Fixing key

* Fixing name

* Moving this back for now

* Updating header

* Restyled by clang-format

* Fixing build

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
woody-apple and restyled-commits authored May 23, 2024
1 parent 45f4939 commit d5a74d0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
9 changes: 3 additions & 6 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<MTRDeviceControllerStorageDelegate> _Nullable)storageDelegate
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 11 additions & 13 deletions src/darwin/Framework/CHIP/MTRDeviceControllerLocalTestStorage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<MTRDeviceControllerStorageDelegate> _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);
}
Expand All @@ -58,7 +56,7 @@ - (instancetype)initWithPassThroughStorage:(id<MTRDeviceControllerStorageDelegat
sharingType:(MTRStorageSharingType)sharingType
{
if (sharingType == MTRStorageSharingTypeNotShared) {
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSData * storedData = [defaults dataForKey:key];
NSError * error;
id value = [NSKeyedUnarchiver unarchivedObjectOfClasses:MTRDeviceControllerStorageClasses() fromData:storedData error:&error];
Expand Down Expand Up @@ -86,7 +84,7 @@ - (BOOL)controller:(MTRDeviceController *)controller
MTR_LOG_ERROR("MTRDeviceControllerLocalTestStorage storeValue: failed to convert value object to data %@", error);
return NO;
}
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:key];
return YES;
} else {
Expand All @@ -105,7 +103,7 @@ - (BOOL)controller:(MTRDeviceController *)controller
sharingType:(MTRStorageSharingType)sharingType
{
if (sharingType == MTRStorageSharingTypeNotShared) {
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:key];
return YES;
} else {
Expand All @@ -121,7 +119,7 @@ - (BOOL)controller:(MTRDeviceController *)controller
- (NSDictionary<NSString *, id<NSSecureCoding>> *)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:)]) {
Expand All @@ -136,7 +134,7 @@ - (BOOL)controller:(MTRDeviceController *)controller
- (BOOL)controller:(MTRDeviceController *)controller storeValues:(NSDictionary<NSString *, id<NSSecureCoding>> *)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;
Expand Down
8 changes: 8 additions & 0 deletions src/darwin/Framework/CHIP/MTRDevice_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 5 additions & 8 deletions src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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];
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/platform/Darwin/UserDefaults.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@

#import <Foundation/Foundation.h>

static NSString * const kUserDefaultDomain = @"org.csa-iot.matter.darwin";
static NSString * const kSRPTimeoutInMsecsUserDefaultKey = @"SRPTimeoutInMSecsOverride";

namespace chip {
namespace Platform {

std::optional<uint16_t> GetUserDefaultDnssdSRPTimeoutInMSecs()
{
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kUserDefaultDomain];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSInteger srpTimeoutValue = [defaults integerForKey:kSRPTimeoutInMsecsUserDefaultKey];
if (CanCastTo<uint16_t>(srpTimeoutValue)) {
uint16_t timeoutinMsecs = static_cast<uint16_t>(srpTimeoutValue);
Expand Down

0 comments on commit d5a74d0

Please sign in to comment.