Skip to content

Implement NSSecureCoding for GDLLogStorage #2216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 17 additions & 1 deletion GoogleDataLogger/Tests/GDLLogStorageTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down