From 5bb58c064d70b9efbd711f67bc360265711c7e41 Mon Sep 17 00:00:00 2001 From: Michael Haney Date: Mon, 24 Dec 2018 15:51:02 -0800 Subject: [PATCH 1/3] Implement NSSecureCoding for GDLLogStorage --- .../GoogleDataLogger/Classes/GDLLogStorage.m | 24 ++++++++++++++++--- GoogleDataLogger/Tests/GDLLogStorageTest.m | 18 +++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m b/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m index 4bae7aad462..d00f0c30306 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 kGDLLogTargetToLogsKey = @"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:kGDLLogTargetToLogsKey]; + }); + 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:kGDLLogTargetToLogsKey]; + }); } @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. */ From 679edf76c03cf5753e9c2e7ef519438ef68dedb5 Mon Sep 17 00:00:00 2001 From: Michael Haney Date: Mon, 24 Dec 2018 15:53:08 -0800 Subject: [PATCH 2/3] Fix style --- .../GoogleDataLogger/Classes/GDLLogStorage.m | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m b/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m index d00f0c30306..e2f2790c145 100644 --- a/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m +++ b/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m @@ -181,10 +181,10 @@ - (void)addLogToTrackingCollections:(GDLLogEvent *)log logFile:(NSURL *)logFile #pragma mark - NSSecureCoding /** The NSKeyedCoder key for the logHashToFile property. */ -static NSString * const kGDLLogHashToLogFileKey = @"logHashToLogFileKey"; +static NSString *const kGDLLogHashToLogFileKey = @"logHashToLogFileKey"; /** The NSKeyedCoder key for the logTargetToLogFileSet property. */ -static NSString * const kGDLLogTargetToLogsKey = @"logTargetToLogFileSetKey"; +static NSString *const kGDLLogTargetToLogsKey = @"logTargetToLogFileSetKey"; + (BOOL)supportsSecureCoding { return YES; @@ -195,10 +195,10 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder { 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:kGDLLogTargetToLogsKey]; + sharedInstance->_logHashToLogFile = + [aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogHashToLogFileKey]; + sharedInstance->_logTargetToLogFileSet = + [aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogTargetToLogsKey]; }); return sharedInstance; } From 2525051e55be07a30afa405f8575133be106f123 Mon Sep 17 00:00:00 2001 From: Michael Haney Date: Mon, 24 Dec 2018 15:54:09 -0800 Subject: [PATCH 3/3] Rename variable --- GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m b/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m index e2f2790c145..bf456a92057 100644 --- a/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m +++ b/GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m @@ -184,7 +184,7 @@ - (void)addLogToTrackingCollections:(GDLLogEvent *)log logFile:(NSURL *)logFile static NSString *const kGDLLogHashToLogFileKey = @"logHashToLogFileKey"; /** The NSKeyedCoder key for the logTargetToLogFileSet property. */ -static NSString *const kGDLLogTargetToLogsKey = @"logTargetToLogFileSetKey"; +static NSString *const kGDLLogTargetToLogSetKey = @"logTargetToLogFileSetKey"; + (BOOL)supportsSecureCoding { return YES; @@ -198,7 +198,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder { sharedInstance->_logHashToLogFile = [aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogHashToLogFileKey]; sharedInstance->_logTargetToLogFileSet = - [aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogTargetToLogsKey]; + [aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogTargetToLogSetKey]; }); return sharedInstance; } @@ -207,7 +207,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder { GDLLogStorage *sharedInstance = [self.class sharedInstance]; dispatch_sync(sharedInstance.storageQueue, ^{ [aCoder encodeObject:sharedInstance->_logHashToLogFile forKey:kGDLLogHashToLogFileKey]; - [aCoder encodeObject:sharedInstance->_logTargetToLogFileSet forKey:kGDLLogTargetToLogsKey]; + [aCoder encodeObject:sharedInstance->_logTargetToLogFileSet forKey:kGDLLogTargetToLogSetKey]; }); }