Skip to content

Implement NSSecureCoding protocol for GDLLogEvent #2191

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 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions GoogleDataLogger/GoogleDataLogger/Classes/GDLLogEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ typedef NS_ENUM(NSInteger, GDLLogQoS) {
GDLLogQoSFast = 4
};

@interface GDLLogEvent : NSObject
@interface GDLLogEvent : NSObject <NSSecureCoding>

/** The log map identifier, to allow backends to map the extension property to a proto. */
@property(readonly, nonatomic) NSString *logMapID;
Expand All @@ -49,7 +49,7 @@ typedef NS_ENUM(NSInteger, GDLLogQoS) {

/** The log object itself, encapsulated in the transport of your choice, as long as it implements
* the GDLLogProto protocol. */
@property(nonatomic) id<GDLLogProto> extension;
@property(nonatomic) NSData *extensionData;

/** The quality of service tier this log belongs to. */
@property(nonatomic) GDLLogQoS qosTier;
Expand Down
32 changes: 32 additions & 0 deletions GoogleDataLogger/GoogleDataLogger/Classes/GDLLogEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,36 @@ - (instancetype)initWithLogMapID:(NSString *)logMapID logTarget:(NSInteger)logTa
return self;
}

#pragma mark - NSSecureCoding and NSCoding Protocols

+ (BOOL)supportsSecureCoding {
return YES;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
NSString *logMapID = [aDecoder decodeObjectOfClass:[NSObject class] forKey:@"_logMapID"];
NSInteger logTarget = [aDecoder decodeIntegerForKey:@"_logTarget"];
self = [self initWithLogMapID:logMapID logTarget:logTarget];
if (self) {
_extensionData = [aDecoder decodeObjectOfClass:[NSData class] forKey:@"_extensionData"];
_qosTier = [aDecoder decodeIntegerForKey:@"_qosTier"];
_clockSnapshot.timeMillis = [aDecoder decodeInt64ForKey:@"clockSnapshotTimeMillis"];
_clockSnapshot.uptimeMillis = [aDecoder decodeInt64ForKey:@"clockSnapshotUpTimeMillis"];
_clockSnapshot.timezoneOffsetMillis =
[aDecoder decodeInt64ForKey:@"clockSnapshotTimezoneOffsetMillis"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_logMapID forKey:@"_logMapID"];
[aCoder encodeInteger:_logTarget forKey:@"_logTarget"];
[aCoder encodeObject:_extensionData forKey:@"_extensionData"];
[aCoder encodeInteger:_qosTier forKey:@"_qosTier"];
[aCoder encodeInt64:_clockSnapshot.timeMillis forKey:@"clockSnapshotTimeMillis"];
[aCoder encodeInt64:_clockSnapshot.uptimeMillis forKey:@"clockSnapshotUpTimeMillis"];
[aCoder encodeInt64:_clockSnapshot.timezoneOffsetMillis
forKey:@"clockSnapshotTimezoneOffsetMillis"];
}

@end
24 changes: 24 additions & 0 deletions GoogleDataLogger/Tests/GDLLogEventTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,33 @@ @interface GDLLogEventTest : XCTestCase

@implementation GDLLogEventTest

/** Tests the designated initializer. */
- (void)testInit {
XCTAssertNotNil([[GDLLogEvent alloc] initWithLogMapID:@"1" logTarget:1]);
XCTAssertThrows([[GDLLogEvent alloc] initWithLogMapID:@"" logTarget:1]);
}

/** Tests NSKeyedArchiver encoding and decoding. */
- (void)testArchiving {
XCTAssertTrue([GDLLogEvent supportsSecureCoding]);
GDLLogClockSnapshot clockSnapshot = {10, 100, 1000};
GDLLogEvent *logEvent = [[GDLLogEvent alloc] initWithLogMapID:@"testID" logTarget:42];
logEvent.extensionData = [@"someData" dataUsingEncoding:NSUTF8StringEncoding];
logEvent.qosTier = GDLLogQoSTelemetry;
logEvent.clockSnapshot = clockSnapshot;

NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:logEvent];
logEvent = nil;

logEvent = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
XCTAssertEqualObjects(logEvent.logMapID, @"testID");
XCTAssertEqual(logEvent.logTarget, 42);
XCTAssertEqualObjects(logEvent.extensionData,
[@"someData" dataUsingEncoding:NSUTF8StringEncoding]);
XCTAssertEqual(logEvent.qosTier, GDLLogQoSTelemetry);
XCTAssertEqual(logEvent.clockSnapshot.timeMillis, 10);
XCTAssertEqual(logEvent.clockSnapshot.uptimeMillis, 100);
XCTAssertEqual(logEvent.clockSnapshot.timezoneOffsetMillis, 1000);
}

@end