diff --git a/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m b/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m index 4bae7aad462..bf456a92057 100644 --- a/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m +++ b/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m @@ -180,17 +180,35 @@ - (void)addLogToTrackingCollections:(GDLLogEvent *)log logFile:(NSURL *)logFile #pragma mark - NSSecureCoding +/** The NSKeyedCoder key for the logHashToFile property. */ +static NSString *const kGDLLogHashToLogFileKey = @"logHashToLogFileKey"; + +/** The NSKeyedCoder key for the logTargetToLogFileSet property. */ +static NSString *const kGDLLogTargetToLogSetKey = @"logTargetToLogFileSetKey"; + + (BOOL)supportsSecureCoding { return YES; } - (instancetype)initWithCoder:(NSCoder *)aDecoder { - // TODO - return [self.class sharedInstance]; + // Create the singleton and populate its ivars. + GDLLogStorage *sharedInstance = [self.class sharedInstance]; + dispatch_sync(sharedInstance.storageQueue, ^{ + Class NSMutableDictionaryClass = [NSMutableDictionary class]; + sharedInstance->_logHashToLogFile = + [aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogHashToLogFileKey]; + sharedInstance->_logTargetToLogFileSet = + [aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogTargetToLogSetKey]; + }); + return sharedInstance; } - (void)encodeWithCoder:(NSCoder *)aCoder { - // TODO + GDLLogStorage *sharedInstance = [self.class sharedInstance]; + dispatch_sync(sharedInstance.storageQueue, ^{ + [aCoder encodeObject:sharedInstance->_logHashToLogFile forKey:kGDLLogHashToLogFileKey]; + [aCoder encodeObject:sharedInstance->_logTargetToLogFileSet forKey:kGDLLogTargetToLogSetKey]; + }); } @end diff --git a/GoogleDataLogger/Tests/GDLLogStorageTest.m b/GoogleDataLogger/Tests/GDLLogStorageTest.m index b5387ccbb07..53eb80e2def 100644 --- a/GoogleDataLogger/Tests/GDLLogStorageTest.m +++ b/GoogleDataLogger/Tests/GDLLogStorageTest.m @@ -116,7 +116,23 @@ - (void)testLogEventDeallocationIsEnforced { /** Tests encoding and decoding the storage singleton correctly. */ - (void)testNSSecureCoding { - // TODO + GDLLogEvent *logEvent = [[GDLLogEvent alloc] initWithLogMapID:@"404" logTarget:logTarget]; + logEvent.extensionBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; + NSUInteger logHash = logEvent.hash; + XCTAssertNoThrow([[GDLLogStorage sharedInstance] storeLog:logEvent]); + logEvent = nil; + NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDLLogStorage sharedInstance]]; + dispatch_sync([GDLLogStorage sharedInstance].storageQueue, ^{ + XCTAssertNotNil([GDLLogStorage sharedInstance].logHashToLogFile[@(logHash)]); + }); + [[GDLLogStorage sharedInstance] removeLog:@(logHash) logTarget:@(logTarget)]; + dispatch_sync([GDLLogStorage sharedInstance].storageQueue, ^{ + XCTAssertNil([GDLLogStorage sharedInstance].logHashToLogFile[@(logHash)]); + }); + + // TODO(mikehaney24): Ensure that the object created by alloc is discarded? + [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; + XCTAssertNotNil([GDLLogStorage sharedInstance].logHashToLogFile[@(logHash)]); } /** Tests logging a fast log causes an upload attempt. */