Skip to content

Commit

Permalink
[ObjC] More tests around unknown to known failure cases.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 659715244
  • Loading branch information
thomasvl committed Aug 12, 2024
1 parent 49dc63b commit e822dce
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
7 changes: 4 additions & 3 deletions objectivec/GPBMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,11 @@ CF_EXTERN_C_END
* If the intent is to *replace* the message's unknown fields, call `-clearUnknownFields` first.
*
* Since the data from the GPBUnknownFields will always be well formed, this call will almost never
* fail. What could cause it to fail is if the GPBUnknownFields contains a field values it is
* and error for the message's schema - i.e.: if it contains a length delimited field where the
* fail. What could cause it to fail is if the GPBUnknownFields contains a field value that is
* an error for the message's schema - i.e.: if it contains a length delimited field where the
* field number for the message is defined to be a _string_ field, however the length delimited
* data provide is not a valid UTF8 string.
* data provide is not a valid UTF8 string, or if the field is a _packed_ number field, but the
* data provided is not a valid for that field.
*
* @param unknownFields The unknown fields to merge the data from.
* @param extensionRegistry The extension registry to use to look up extensions, can be `nil`.
Expand Down
51 changes: 50 additions & 1 deletion objectivec/Tests/GPBUnknownFieldsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -911,16 +911,32 @@ - (void)testMismatchedFieldTypes {
}

- (void)testMergeFailures {
// Valid data, pushes to the string just fine.
// Valid data, pushes to the fields just fine.
{
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
[ufs addFieldNumber:TestAllTypes_FieldNumber_OptionalString
lengthDelimited:DataFromCStr("abc")];
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedInt32Array
lengthDelimited:DataFromBytes(0x01, 0x02)];
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed32Array
lengthDelimited:DataFromBytes(0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00)];
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed64Array
lengthDelimited:DataFromBytes(0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00)];
TestAllTypes* msg = [TestAllTypes message];
NSError* error = nil;
XCTAssertTrue([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
XCTAssertNil(error);
XCTAssertEqualObjects(msg.optionalString, @"abc");
XCTAssertEqual(msg.repeatedInt32Array.count, 2);
XCTAssertEqual([msg.repeatedInt32Array valueAtIndex:0], 1);
XCTAssertEqual([msg.repeatedInt32Array valueAtIndex:1], 2);
XCTAssertEqual(msg.repeatedFixed32Array.count, 2);
XCTAssertEqual([msg.repeatedFixed32Array valueAtIndex:0], 3);
XCTAssertEqual([msg.repeatedFixed32Array valueAtIndex:1], 4);
XCTAssertEqual(msg.repeatedFixed64Array.count, 2);
XCTAssertEqual([msg.repeatedFixed64Array valueAtIndex:0], 5);
XCTAssertEqual([msg.repeatedFixed64Array valueAtIndex:1], 6);
}

// Invalid UTF-8 causes a failure when pushed to the message.
Expand All @@ -933,6 +949,39 @@ - (void)testMergeFailures {
XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
XCTAssertNotNil(error);
}

// Invalid packed varint causes a failure when pushed to the message.
{
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedInt32Array
lengthDelimited:DataFromBytes(0xff)]; // Invalid varint
TestAllTypes* msg = [TestAllTypes message];
NSError* error = nil;
XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
XCTAssertNotNil(error);
}

// Invalid packed fixed32 causes a failure when pushed to the message.
{
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed32Array
lengthDelimited:DataFromBytes(0x01, 0x00, 0x00)]; // Truncated fixed32
TestAllTypes* msg = [TestAllTypes message];
NSError* error = nil;
XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
XCTAssertNotNil(error);
}

// Invalid packed fixed64 causes a failure when pushed to the message.
{
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
[ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed64Array
lengthDelimited:DataFromBytes(0x01, 0x00, 0x00, 0x00, 0x00)]; // Truncated fixed64
TestAllTypes* msg = [TestAllTypes message];
NSError* error = nil;
XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]);
XCTAssertNotNil(error);
}
}

- (void)testLargeVarint {
Expand Down

0 comments on commit e822dce

Please sign in to comment.