-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Darwin] MTRDevice attribute cache persistent storage local test faci…
…lity (#32181) * [Darwin] MTRDevice attribute cache persistent storage local test facility * Fix header scope * Fix CI compilation issue * Added MTR_PER_CONTROLLER_STORAGE_ENABLED check to fix darwin CI * Fix for the previous fix - now double tested
- Loading branch information
1 parent
c13b324
commit 2f2c4f1
Showing
8 changed files
with
313 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/darwin/Framework/CHIP/MTRDeviceControllerLocalTestStorage.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
/** | ||
* 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> | ||
|
||
#if MTR_PER_CONTROLLER_STORAGE_ENABLED | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
MTR_EXTERN @interface MTRDeviceControllerLocalTestStorage : NSObject<MTRDeviceControllerStorageDelegate> | ||
|
||
// Setting this variable only affects subsequent MTRDeviceController initializations | ||
@property (class, nonatomic, assign) BOOL localTestStorageEnabled; | ||
|
||
// This storage persists items to NSUserDefaults for MTRStorageSharingTypeNotShared data. Items with other sharing types will be droppped, or stored/fetched with the "passthrough storage" if one is specified. | ||
- (instancetype)initWithPassThroughStorage:(id<MTRDeviceControllerStorageDelegate> _Nullable)passThroughStorage; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END | ||
|
||
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED |
97 changes: 97 additions & 0 deletions
97
src/darwin/Framework/CHIP/MTRDeviceControllerLocalTestStorage.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// | ||
/** | ||
* 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 "MTRDeviceControllerLocalTestStorage.h" | ||
|
||
#if MTR_PER_CONTROLLER_STORAGE_ENABLED | ||
|
||
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]; | ||
} | ||
|
||
+ (void)setLocalTestStorageEnabled:(BOOL)localTestStorageEnabled | ||
{ | ||
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; | ||
[defaults setBool:localTestStorageEnabled forKey:kLocalTestUserDefaultEnabledKey]; | ||
} | ||
|
||
- (instancetype)initWithPassThroughStorage:(id<MTRDeviceControllerStorageDelegate>)passThroughStorage | ||
{ | ||
if (self = [super init]) { | ||
_passThroughStorage = passThroughStorage; | ||
} | ||
return self; | ||
} | ||
|
||
- (nullable id<NSSecureCoding>)controller:(MTRDeviceController *)controller | ||
valueForKey:(NSString *)key | ||
securityLevel:(MTRStorageSecurityLevel)securityLevel | ||
sharingType:(MTRStorageSharingType)sharingType | ||
{ | ||
if (sharingType == MTRStorageSharingTypeNotShared) { | ||
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; | ||
NSData * storedData = [defaults dataForKey:key]; | ||
NSError * error; | ||
id value = [NSKeyedUnarchiver unarchivedObjectOfClasses:MTRDeviceControllerStorageClasses() fromData:storedData error:&error]; | ||
return value; | ||
} else { | ||
return [_passThroughStorage controller:controller valueForKey:key securityLevel:securityLevel sharingType:sharingType]; | ||
} | ||
} | ||
|
||
- (BOOL)controller:(MTRDeviceController *)controller | ||
storeValue:(id<NSSecureCoding>)value | ||
forKey:(NSString *)key | ||
securityLevel:(MTRStorageSecurityLevel)securityLevel | ||
sharingType:(MTRStorageSharingType)sharingType | ||
{ | ||
if (sharingType == MTRStorageSharingTypeNotShared) { | ||
NSError * error; | ||
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:value requiringSecureCoding:YES error:&error]; | ||
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; | ||
[defaults setObject:data forKey:key]; | ||
return YES; | ||
} else { | ||
return [_passThroughStorage controller:controller storeValue:value forKey:key securityLevel:securityLevel sharingType:sharingType]; | ||
} | ||
} | ||
|
||
- (BOOL)controller:(MTRDeviceController *)controller | ||
removeValueForKey:(NSString *)key | ||
securityLevel:(MTRStorageSecurityLevel)securityLevel | ||
sharingType:(MTRStorageSharingType)sharingType | ||
{ | ||
if (sharingType == MTRStorageSharingTypeNotShared) { | ||
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; | ||
[defaults removeObjectForKey:key]; | ||
return YES; | ||
} else { | ||
return [_passThroughStorage controller:controller removeValueForKey:key securityLevel:securityLevel sharingType:sharingType]; | ||
} | ||
} | ||
@end | ||
|
||
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.