From 1a300ce648d08fd92b9f716aa29005cbf4be6daf Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Mon, 5 Aug 2024 16:11:08 -0700 Subject: [PATCH] [ObjC] More tests around unknown to known failure cases. PiperOrigin-RevId: 659715244 --- objectivec/GPBMessage.h | 7 ++-- objectivec/Tests/GPBUnknownFieldsTest.m | 51 ++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/objectivec/GPBMessage.h b/objectivec/GPBMessage.h index 90d6991de275..2d9b67ebd33a 100644 --- a/objectivec/GPBMessage.h +++ b/objectivec/GPBMessage.h @@ -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`. diff --git a/objectivec/Tests/GPBUnknownFieldsTest.m b/objectivec/Tests/GPBUnknownFieldsTest.m index c1e5e10634a8..42c3f7dc87e6 100644 --- a/objectivec/Tests/GPBUnknownFieldsTest.m +++ b/objectivec/Tests/GPBUnknownFieldsTest.m @@ -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. @@ -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 {