Skip to content

Commit dfa3887

Browse files
authored
Implement NSSecureCoding for GDLLogStorage (#2216)
* Implement NSSecureCoding for GDLLogStorage * Fix style * Rename variable
1 parent bbd0d30 commit dfa3887

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,35 @@ - (void)addLogToTrackingCollections:(GDLLogEvent *)log logFile:(NSURL *)logFile
180180

181181
#pragma mark - NSSecureCoding
182182

183+
/** The NSKeyedCoder key for the logHashToFile property. */
184+
static NSString *const kGDLLogHashToLogFileKey = @"logHashToLogFileKey";
185+
186+
/** The NSKeyedCoder key for the logTargetToLogFileSet property. */
187+
static NSString *const kGDLLogTargetToLogSetKey = @"logTargetToLogFileSetKey";
188+
183189
+ (BOOL)supportsSecureCoding {
184190
return YES;
185191
}
186192

187193
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
188-
// TODO
189-
return [self.class sharedInstance];
194+
// Create the singleton and populate its ivars.
195+
GDLLogStorage *sharedInstance = [self.class sharedInstance];
196+
dispatch_sync(sharedInstance.storageQueue, ^{
197+
Class NSMutableDictionaryClass = [NSMutableDictionary class];
198+
sharedInstance->_logHashToLogFile =
199+
[aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogHashToLogFileKey];
200+
sharedInstance->_logTargetToLogFileSet =
201+
[aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogTargetToLogSetKey];
202+
});
203+
return sharedInstance;
190204
}
191205

192206
- (void)encodeWithCoder:(NSCoder *)aCoder {
193-
// TODO
207+
GDLLogStorage *sharedInstance = [self.class sharedInstance];
208+
dispatch_sync(sharedInstance.storageQueue, ^{
209+
[aCoder encodeObject:sharedInstance->_logHashToLogFile forKey:kGDLLogHashToLogFileKey];
210+
[aCoder encodeObject:sharedInstance->_logTargetToLogFileSet forKey:kGDLLogTargetToLogSetKey];
211+
});
194212
}
195213

196214
@end

GoogleDataLogger/Tests/GDLLogStorageTest.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,23 @@ - (void)testLogEventDeallocationIsEnforced {
116116

117117
/** Tests encoding and decoding the storage singleton correctly. */
118118
- (void)testNSSecureCoding {
119-
// TODO
119+
GDLLogEvent *logEvent = [[GDLLogEvent alloc] initWithLogMapID:@"404" logTarget:logTarget];
120+
logEvent.extensionBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding];
121+
NSUInteger logHash = logEvent.hash;
122+
XCTAssertNoThrow([[GDLLogStorage sharedInstance] storeLog:logEvent]);
123+
logEvent = nil;
124+
NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDLLogStorage sharedInstance]];
125+
dispatch_sync([GDLLogStorage sharedInstance].storageQueue, ^{
126+
XCTAssertNotNil([GDLLogStorage sharedInstance].logHashToLogFile[@(logHash)]);
127+
});
128+
[[GDLLogStorage sharedInstance] removeLog:@(logHash) logTarget:@(logTarget)];
129+
dispatch_sync([GDLLogStorage sharedInstance].storageQueue, ^{
130+
XCTAssertNil([GDLLogStorage sharedInstance].logHashToLogFile[@(logHash)]);
131+
});
132+
133+
// TODO(mikehaney24): Ensure that the object created by alloc is discarded?
134+
[NSKeyedUnarchiver unarchiveObjectWithData:storageData];
135+
XCTAssertNotNil([GDLLogStorage sharedInstance].logHashToLogFile[@(logHash)]);
120136
}
121137

122138
/** Tests logging a fast log causes an upload attempt. */

0 commit comments

Comments
 (0)