diff options
author | Thomas Van Lenten <thomasvl@google.com> | 2016-04-06 15:52:17 -0400 |
---|---|---|
committer | Thomas Van Lenten <thomasvl@google.com> | 2016-04-06 15:52:17 -0400 |
commit | 28c5c25397030aa5db5c2ad33bad58f2fa50354a (patch) | |
tree | bde8d18a616fa07e84f5b4faf6a01411797991dc /objectivec/Tests | |
parent | f98d2f5147bdb8182bc63acf00c9be9be5956b5e (diff) | |
parent | c9167f2acdd8ab477acb620cdd0bd231c8a0f3d0 (diff) | |
download | protobuf-28c5c25397030aa5db5c2ad33bad58f2fa50354a.tar.gz protobuf-28c5c25397030aa5db5c2ad33bad58f2fa50354a.tar.bz2 protobuf-28c5c25397030aa5db5c2ad33bad58f2fa50354a.zip |
Merge pull request #1391 from thomasvl/string_tweaks
ObjC String followups: one test case only addition, behavior change for invalid UTF-8
Diffstat (limited to 'objectivec/Tests')
-rw-r--r-- | objectivec/Tests/GPBCodedInputStreamTests.m | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/objectivec/Tests/GPBCodedInputStreamTests.m b/objectivec/Tests/GPBCodedInputStreamTests.m index b0e39d2c..cc402156 100644 --- a/objectivec/Tests/GPBCodedInputStreamTests.m +++ b/objectivec/Tests/GPBCodedInputStreamTests.m @@ -283,16 +283,53 @@ [output writeRawData:[NSData dataWithBytes:bytes length:sizeof(bytes)]]; [output flush]; - NSData* data = + NSData *data = [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; + NSError *error = nil; TestAllTypes* message = [TestAllTypes parseFromCodedInputStream:input extensionRegistry:nil - error:NULL]; - XCTAssertNotNil(message); - // Make sure we can read string properties twice without crashing. - XCTAssertEqual([message.defaultString length], (NSUInteger)0); - XCTAssertEqualObjects(@"", message.defaultString); + error:&error]; + XCTAssertNotNil(error); + XCTAssertNil(message); +} + +- (void)testBOMWithinStrings { + // We've seen servers that end up with BOMs within strings (not always at the + // start, and sometimes in multiple places), make sure they always parse + // correctly. (Again, this is inpart incase a custom string class is ever + // used again.) + const char* strs[] = { + "\xEF\xBB\xBF String with BOM", + "String with \xEF\xBB\xBF in middle", + "String with end bom \xEF\xBB\xBF", + "\xEF\xBB\xBF\xe2\x99\xa1", // BOM White Heart + "\xEF\xBB\xBF\xEF\xBB\xBF String with Two BOM", + }; + for (size_t i = 0; i < GPBARRAYSIZE(strs); ++i) { + NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; + GPBCodedOutputStream* output = + [GPBCodedOutputStream streamWithOutputStream:rawOutput]; + + int32_t tag = GPBWireFormatMakeTag(TestAllTypes_FieldNumber_DefaultString, + GPBWireFormatLengthDelimited); + [output writeRawVarint32:tag]; + size_t length = strlen(strs[i]); + [output writeRawVarint32:(int32_t)length]; + [output writeRawData:[NSData dataWithBytes:strs[i] length:length]]; + [output flush]; + + NSData* data = + [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; + GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; + TestAllTypes* message = [TestAllTypes parseFromCodedInputStream:input + extensionRegistry:nil + error:NULL]; + XCTAssertNotNil(message, @"Loop %zd", i); + // Ensure the string is there. NSString can consume the BOM in some + // cases, so don't actually check the string for exact equality. + XCTAssertTrue(message.defaultString.length > 0, @"Loop %zd", i); + } } @end |