From 6ec27f0d0847ef4531c9ab4ee5174591d9a391b0 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Thu, 29 Aug 2019 17:09:02 -0400 Subject: [PATCH] Add an explicit test using secure coding for added safety. --- objectivec/Tests/GPBMessageTests.m | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/objectivec/Tests/GPBMessageTests.m b/objectivec/Tests/GPBMessageTests.m index 4d0f7f8d0..81bf5f6c8 100644 --- a/objectivec/Tests/GPBMessageTests.m +++ b/objectivec/Tests/GPBMessageTests.m @@ -338,14 +338,38 @@ } - (void)testCoding { + GPBMessage *original = [self mergeResult]; NSData *data = - [NSKeyedArchiver archivedDataWithRootObject:[self mergeResult]]; + [NSKeyedArchiver archivedDataWithRootObject:original]; id unarchivedObject = [NSKeyedUnarchiver unarchiveObjectWithData:data]; - XCTAssertEqualObjects(unarchivedObject, [self mergeResult]); + XCTAssertEqualObjects(unarchivedObject, original); // Intentionally doing a pointer comparison. - XCTAssertNotEqual(unarchivedObject, [self mergeResult]); + XCTAssertNotEqual(unarchivedObject, original); +} + +- (void)testSecureCoding { + GPBMessage *original = [self mergeResult]; + + NSString *key = @"testing123"; + + NSMutableData *data = [NSMutableData data]; + NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; + [archiver setRequiresSecureCoding:YES]; + [archiver encodeObject:obj forKey:key]; + [archiver finishEncoding]; + + NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; + [unarchiver setRequiresSecureCoding:YES]; + id unarchivedObject = [unarchiver decodeObjectOfClass:[GPBMessage class] + forKey:key]; + [unarchiver finishDecoding]; + + XCTAssertEqualObjects(unarchivedObject, original); + + // Intentionally doing a pointer comparison. + XCTAssertNotEqual(unarchivedObject, original); } - (void)testObjectReset {