@ -6,6 +6,7 @@
/ / https : / / developers . google . com / open - source / licenses / bsd
#import "GPBTestUtilities . h "
#import "GPBUnknownFieldSet . h "
#import "GPBUnknownFieldSet_PackagePrivate . h "
#import "GPBUnknownField_PackagePrivate . h "
@ -376,6 +377,180 @@
XCTAssertEqual ( 0 x7FFFFFFFFFFFFFFFULL , [ field2 . varintList valueAtIndex : 0 ] ) ;
}
static NSData * DataForGroupsOfDepth ( NSUInteger depth ) {
NSMutableData * data = [ NSMutableData dataWithCapacity : 0 ] ;
uint32_t byte = 35 ; / / 35 = 0 b100011 - > field 4 / start group
for ( NSUInteger i = 0 ; i < depth ; + + i ) {
[ data appendBytes : & byte length : 1 ] ;
}
byte = 8 ; / / 8 = 0 b1000 , - > field 1 / varint
[ data appendBytes : & byte length : 1 ] ;
byte = 1 ; / / 1 - > varint value of 1
[ data appendBytes : & byte length : 1 ] ;
byte = 36 ; / / 36 = 0 b100100 - > field 4 / end group
for ( NSUInteger i = 0 ; i < depth ; + + i ) {
[ data appendBytes : & byte length : 1 ] ;
}
return data ;
}
- ( void ) testParsingNestingGroupData {
/ / 35 = 0 b100011 - > field 4 / start group
/ / 36 = 0 b100100 - > field 4 / end group
/ / 43 = 0 b101011 - > field 5 / end group
/ / 44 = 0 b101100 - > field 5 / end group
/ / 8 = 0 b1000 , 1 - > field 1 / varint , value of 1
/ / 21 = 0 b10101 , 0 x78 , 0 x56 , 0 x34 , 0 x12 - > field 2 / fixed32 , value of 0 x12345678
/ / 25 = 0 b11001 , 0 xf0 , 0 xde , 0 xbc , 0 x9a , 0 x78 , 0 x56 , 0 x34 , 0 x12 - > field 3 / fixed64 ,
/ / value of 0 x123456789abcdef0LL
/ / 50 = 0 b110010 , 0 x0 - > field 6 / length delimited , length 0
/ / 50 = 0 b110010 , 0 x1 , 42 - > field 6 / length delimited , length 1 , byte 42
/ / 0 - > field 0 which is invalid / varint
/ / 15 = 0 b1111 - > field 1 , wire type 7 which is invalid
TestEmptyMessage * m = [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 36 )
error : NULL ] ; / / empty group
XCTAssertEqual ( m . unknownFields . countOfFields , ( NSUInteger ) 1 ) ;
GPBUnknownField * field = [ m . unknownFields getField : 4 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
GPBUnknownFieldSet * group = field . groupList [ 0 ] ;
XCTAssertEqual ( group . countOfFields , ( NSUInteger ) 0 ) ;
m = [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 8 , 1 , 36 ) error : NULL ] ; / / varint
XCTAssertEqual ( m . unknownFields . countOfFields , ( NSUInteger ) 1 ) ;
field = [ m . unknownFields getField : 4 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
field = [ group getField : 1 ] ;
XCTAssertEqual ( field . varintList . count , ( NSUInteger ) 1 ) ;
XCTAssertEqual ( [ field . varintList valueAtIndex : 0 ] , 1 ) ;
m = [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 21 , 0 x78 , 0 x56 , 0 x34 , 0 x12 , 36 )
error : NULL ] ; / / fixed32
XCTAssertEqual ( m . unknownFields . countOfFields , ( NSUInteger ) 1 ) ;
field = [ m . unknownFields getField : 4 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
field = [ group getField : 2 ] ;
XCTAssertEqual ( field . fixed32List . count , ( NSUInteger ) 1 ) ;
XCTAssertEqual ( [ field . fixed32List valueAtIndex : 0 ] , 0 x12345678 ) ;
m = [ TestEmptyMessage
parseFromData : DataFromBytes ( 35 , 25 , 0 xf0 , 0 xde , 0 xbc , 0 x9a , 0 x78 , 0 x56 , 0 x34 , 0 x12 ,
36 )
error : NULL ] ; / / fixed64
XCTAssertEqual ( m . unknownFields . countOfFields , ( NSUInteger ) 1 ) ;
field = [ m . unknownFields getField : 4 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
field = [ group getField : 3 ] ;
XCTAssertEqual ( field . fixed64List . count , ( NSUInteger ) 1 ) ;
XCTAssertEqual ( [ field . fixed64List valueAtIndex : 0 ] , 0 x123456789abcdef0LL ) ;
m = [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 50 , 0 , 36 )
error : NULL ] ; / / length delimited , length 0
XCTAssertEqual ( m . unknownFields . countOfFields , ( NSUInteger ) 1 ) ;
field = [ m . unknownFields getField : 4 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
field = [ group getField : 6 ] ;
XCTAssertEqual ( field . lengthDelimitedList . count , ( NSUInteger ) 1 ) ;
XCTAssertEqualObjects ( field . lengthDelimitedList [ 0 ] , [ NSData data ] ) ;
m = [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 50 , 1 , 42 , 36 )
error : NULL ] ; / / length delimited , length 1 , byte 42
XCTAssertEqual ( m . unknownFields . countOfFields , ( NSUInteger ) 1 ) ;
field = [ m . unknownFields getField : 4 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
field = [ group getField : 6 ] ;
XCTAssertEqual ( field . lengthDelimitedList . count , ( NSUInteger ) 1 ) ;
XCTAssertEqualObjects ( field . lengthDelimitedList [ 0 ] , DataFromBytes ( 42 ) ) ;
m = [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 43 , 44 , 36 ) error : NULL ] ; / / Sub group
field = [ m . unknownFields getField : 4 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
XCTAssertEqual ( group . countOfFields , ( NSUInteger ) 1 ) ;
field = [ group getField : 5 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
XCTAssertEqual ( group . countOfFields , ( NSUInteger ) 0 ) ;
m = [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 8 , 1 , 43 , 8 , 2 , 44 , 36 )
error : NULL ] ; / / varint and sub group with varint
XCTAssertEqual ( m . unknownFields . countOfFields , ( NSUInteger ) 1 ) ;
field = [ m . unknownFields getField : 4 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
XCTAssertEqual ( group . countOfFields , ( NSUInteger ) 2 ) ;
field = [ group getField : 1 ] ;
XCTAssertEqual ( field . varintList . count , ( NSUInteger ) 1 ) ;
XCTAssertEqual ( [ field . varintList valueAtIndex : 0 ] , 1 ) ;
field = [ group getField : 5 ] ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
field = [ group getField : 1 ] ;
XCTAssertEqual ( field . varintList . count , ( NSUInteger ) 1 ) ;
XCTAssertEqual ( [ field . varintList valueAtIndex : 0 ] , 2 ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 0 , 36 )
error : NULL ] ) ; / / Invalid field number
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 15 , 36 )
error : NULL ] ) ; / / Invalid wire type
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 21 , 0 x78 , 0 x56 , 0 x34 )
error : NULL ] ) ; / / truncated fixed32
XCTAssertNil ( [ TestEmptyMessage
parseFromData : DataFromBytes ( 35 , 25 , 0 xf0 , 0 xde , 0 xbc , 0 x9a , 0 x78 , 0 x56 ,
0 x34 )
error : NULL ] ) ; / / truncated fixed64
/ / Mising end group
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 ) error : NULL ] ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 8 , 1 ) error : NULL ] ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 43 ) error : NULL ] ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 43 , 8 , 1 ) error : NULL ] ) ;
/ / Wrong end group
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 44 ) error : NULL ] ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 8 , 1 , 44 ) error : NULL ] ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 43 , 36 ) error : NULL ] ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 43 , 8 , 1 , 36 ) error : NULL ] ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 43 , 44 , 44 ) error : NULL ] ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : DataFromBytes ( 35 , 43 , 8 , 1 , 44 , 44 ) error : NULL ] ) ;
/ / This is the same limit as within GPBCodedInputStream .
const NSUInteger kDefaultRecursionLimit = 100 ;
/ / That depth parses .
NSData * testData = DataForGroupsOfDepth ( kDefaultRecursionLimit ) ;
m = [ TestEmptyMessage parseFromData : testData error : NULL ] ;
XCTAssertEqual ( m . unknownFields . countOfFields , ( NSUInteger ) 1 ) ;
field = [ m . unknownFields getField : 4 ] ;
for ( NSUInteger i = 0 ; i < kDefaultRecursionLimit ; + + i ) {
XCTAssertEqual ( field . varintList . count , ( NSUInteger ) 0 ) ;
XCTAssertEqual ( field . fixed32List . count , ( NSUInteger ) 0 ) ;
XCTAssertEqual ( field . fixed64List . count , ( NSUInteger ) 0 ) ;
XCTAssertEqual ( field . lengthDelimitedList . count , ( NSUInteger ) 0 ) ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 1 ) ;
group = field . groupList [ 0 ] ;
XCTAssertEqual ( group . countOfFields , ( NSUInteger ) 1 ) ;
field = [ group getField : ( i < ( kDefaultRecursionLimit - 1 ) ? 4 : 1 ) ] ;
}
/ / field is of the inner most group
XCTAssertEqual ( field . varintList . count , ( NSUInteger ) 1 ) ;
XCTAssertEqual ( [ field . varintList valueAtIndex : 0 ] , ( NSUInteger ) 1 ) ;
XCTAssertEqual ( field . fixed32List . count , ( NSUInteger ) 0 ) ;
XCTAssertEqual ( field . fixed64List . count , ( NSUInteger ) 0 ) ;
XCTAssertEqual ( field . lengthDelimitedList . count , ( NSUInteger ) 0 ) ;
XCTAssertEqual ( field . groupList . count , ( NSUInteger ) 0 ) ;
/ / One more level deep fails .
testData = DataForGroupsOfDepth ( kDefaultRecursionLimit + 1 ) ;
XCTAssertNil ( [ TestEmptyMessage parseFromData : testData error : NULL ] ) ;
}
#pragma mark - Field tests
/ / Some tests directly on fields since the dictionary in FieldSet can gate
/ / testing some of these .