Skip to content

Commit e822dce

Browse files
committed
[ObjC] More tests around unknown to known failure cases.
PiperOrigin-RevId: 659715244
1 parent 49dc63b commit e822dce

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

objectivec/GPBMessage.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,11 @@ CF_EXTERN_C_END
510510
* If the intent is to *replace* the message's unknown fields, call `-clearUnknownFields` first.
511511
*
512512
* Since the data from the GPBUnknownFields will always be well formed, this call will almost never
513-
* fail. What could cause it to fail is if the GPBUnknownFields contains a field values it is
514-
* and error for the message's schema - i.e.: if it contains a length delimited field where the
513+
* fail. What could cause it to fail is if the GPBUnknownFields contains a field value that is
514+
* an error for the message's schema - i.e.: if it contains a length delimited field where the
515515
* field number for the message is defined to be a _string_ field, however the length delimited
516-
* data provide is not a valid UTF8 string.
516+
* data provide is not a valid UTF8 string, or if the field is a _packed_ number field, but the
517+
* data provided is not a valid for that field.
517518
*
518519
* @param unknownFields The unknown fields to merge the data from.
519520
* @param extensionRegistry The extension registry to use to look up extensions, can be `nil`.

objectivec/Tests/GPBUnknownFieldsTest.m

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,16 +911,32 @@ - (void)testMismatchedFieldTypes {
911911
}
912912

913913
- (void)testMergeFailures {
914-
// Valid data, pushes to the string just fine.
914+
// Valid data, pushes to the fields just fine.
915915
{
916916
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
917917
[ufs addFieldNumber:TestAllTypes_FieldNumber_OptionalString
918918
lengthDelimited:DataFromCStr("abc")];
919+
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedInt32Array
920+
lengthDelimited:DataFromBytes(0x01, 0x02)];
921+
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed32Array
922+
lengthDelimited:DataFromBytes(0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00)];
923+
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed64Array
924+
lengthDelimited:DataFromBytes(0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
925+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00)];
919926
TestAllTypes* msg = [TestAllTypes message];
920927
NSError* error = nil;
921928
XCTAssertTrue([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
922929
XCTAssertNil(error);
923930
XCTAssertEqualObjects(msg.optionalString, @"abc");
931+
XCTAssertEqual(msg.repeatedInt32Array.count, 2);
932+
XCTAssertEqual([msg.repeatedInt32Array valueAtIndex:0], 1);
933+
XCTAssertEqual([msg.repeatedInt32Array valueAtIndex:1], 2);
934+
XCTAssertEqual(msg.repeatedFixed32Array.count, 2);
935+
XCTAssertEqual([msg.repeatedFixed32Array valueAtIndex:0], 3);
936+
XCTAssertEqual([msg.repeatedFixed32Array valueAtIndex:1], 4);
937+
XCTAssertEqual(msg.repeatedFixed64Array.count, 2);
938+
XCTAssertEqual([msg.repeatedFixed64Array valueAtIndex:0], 5);
939+
XCTAssertEqual([msg.repeatedFixed64Array valueAtIndex:1], 6);
924940
}
925941

926942
// Invalid UTF-8 causes a failure when pushed to the message.
@@ -933,6 +949,39 @@ - (void)testMergeFailures {
933949
XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
934950
XCTAssertNotNil(error);
935951
}
952+
953+
// Invalid packed varint causes a failure when pushed to the message.
954+
{
955+
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
956+
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedInt32Array
957+
lengthDelimited:DataFromBytes(0xff)]; // Invalid varint
958+
TestAllTypes* msg = [TestAllTypes message];
959+
NSError* error = nil;
960+
XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
961+
XCTAssertNotNil(error);
962+
}
963+
964+
// Invalid packed fixed32 causes a failure when pushed to the message.
965+
{
966+
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
967+
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed32Array
968+
lengthDelimited:DataFromBytes(0x01, 0x00, 0x00)]; // Truncated fixed32
969+
TestAllTypes* msg = [TestAllTypes message];
970+
NSError* error = nil;
971+
XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
972+
XCTAssertNotNil(error);
973+
}
974+
975+
// Invalid packed fixed64 causes a failure when pushed to the message.
976+
{
977+
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
978+
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed64Array
979+
lengthDelimited:DataFromBytes(0x01, 0x00, 0x00, 0x00, 0x00)]; // Truncated fixed64
980+
TestAllTypes* msg = [TestAllTypes message];
981+
NSError* error = nil;
982+
XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
983+
XCTAssertNotNil(error);
984+
}
936985
}
937986

938987
- (void)testLargeVarint {

0 commit comments

Comments
 (0)