1// Protocol Buffers - Google's data interchange format 2// Copyright 2008 Google Inc. All rights reserved. 3// 4// Use of this source code is governed by a BSD-style 5// license that can be found in the LICENSE file or at 6// https://developers.google.com/open-source/licenses/bsd 7 8#import <Foundation/Foundation.h> 9 10#import "GPBCodedInputStream.h" 11#import "GPBCodedInputStream_PackagePrivate.h" 12#import "GPBCodedOutputStream.h" 13#import "GPBTestUtilities.h" 14#import "GPBUnknownField.h" 15#import "GPBUnknownFieldSet_PackagePrivate.h" 16#import "GPBUnknownFields.h" 17#import "GPBUtilities.h" 18#import "GPBUtilities_PackagePrivate.h" 19#import "GPBWireFormat.h" 20#import "objectivec/Tests/Unittest.pbobjc.h" 21 22@interface CodedInputStreamTests : GPBTestCase 23@end 24 25@implementation CodedInputStreamTests 26 27- (NSData*)bytes_with_sentinel:(int32_t)unused, ... { 28 va_list list; 29 va_start(list, unused); 30 31 NSMutableData* values = [NSMutableData dataWithCapacity:0]; 32 int32_t i; 33 34 while ((i = va_arg(list, int32_t)) != 256) { 35 NSAssert(i >= 0 && i < 256, @""); 36 uint8_t u = (uint8_t)i; 37 [values appendBytes:&u length:1]; 38 } 39 40 va_end(list); 41 42 return values; 43} 44 45#define bytes(...) [self bytes_with_sentinel:0, __VA_ARGS__, 256] 46 47- (void)testDecodeZigZag { 48 [self assertReadZigZag32:bytes(0x0) value:0]; 49 [self assertReadZigZag32:bytes(0x1) value:-1]; 50 [self assertReadZigZag32:bytes(0x2) value:1]; 51 [self assertReadZigZag32:bytes(0x3) value:-2]; 52 53 [self assertReadZigZag32:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0x07) value:(int32_t)0x3FFFFFFF]; 54 [self assertReadZigZag32:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x07) value:(int32_t)0xC0000000]; 55 [self assertReadZigZag32:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0x0F) value:(int32_t)0x7FFFFFFF]; 56 [self assertReadZigZag32:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x0F) value:(int32_t)0x80000000]; 57 58 [self assertReadZigZag64:bytes(0x0) value:0]; 59 [self assertReadZigZag64:bytes(0x1) value:-1]; 60 [self assertReadZigZag64:bytes(0x2) value:1]; 61 [self assertReadZigZag64:bytes(0x3) value:-2]; 62 63 [self assertReadZigZag64:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0x07) value:(int32_t)0x3FFFFFFF]; 64 [self assertReadZigZag64:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x07) value:(int32_t)0xC0000000]; 65 [self assertReadZigZag64:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0x0F) value:(int32_t)0x7FFFFFFF]; 66 [self assertReadZigZag64:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x0F) value:(int32_t)0x80000000]; 67 68 [self assertReadZigZag64:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01) 69 value:0x7FFFFFFFFFFFFFFFL]; 70 [self assertReadZigZag64:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01) 71 value:0x8000000000000000L]; 72} 73 74- (void)assertReadVarint:(NSData*)data value:(int64_t)value { 75 if (value <= INT32_MAX && value >= INT32_MIN) { 76 { 77 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 78 XCTAssertEqual((int32_t)value, [input readInt32]); 79 } 80 { 81 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 82 XCTAssertEqual((int32_t)value, [input readEnum]); 83 } 84 } 85 if (value <= UINT32_MAX && value >= 0) { 86 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 87 XCTAssertEqual((uint32_t)value, [input readUInt32]); 88 } 89 { 90 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 91 XCTAssertEqual(value, [input readInt64]); 92 } 93 if (value >= 0) { 94 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 95 XCTAssertEqual((uint64_t)value, [input readUInt64]); 96 } 97} 98 99- (void)assertReadLittleEndian32:(NSData*)data value:(int32_t)value { 100 { 101 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 102 XCTAssertEqual(value, [input readSFixed32]); 103 } 104 { 105 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 106 XCTAssertEqual(GPBConvertInt32ToFloat(value), [input readFloat]); 107 } 108 { 109 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 110 XCTAssertEqual((uint32_t)value, [input readFixed32]); 111 } 112 { 113 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 114 XCTAssertEqual(value, [input readSFixed32]); 115 } 116} 117 118- (void)assertReadLittleEndian64:(NSData*)data value:(int64_t)value { 119 { 120 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 121 XCTAssertEqual(value, [input readSFixed64]); 122 } 123 { 124 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 125 XCTAssertEqual(GPBConvertInt64ToDouble(value), [input readDouble]); 126 } 127 { 128 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 129 XCTAssertEqual((uint64_t)value, [input readFixed64]); 130 } 131 { 132 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 133 XCTAssertEqual(value, [input readSFixed64]); 134 } 135} 136 137- (void)assertReadZigZag32:(NSData*)data value:(int64_t)value { 138 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 139 XCTAssertEqual((int32_t)value, [input readSInt32]); 140} 141 142- (void)assertReadZigZag64:(NSData*)data value:(int64_t)value { 143 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 144 XCTAssertEqual(value, [input readSInt64]); 145} 146 147- (void)assertReadVarintFailure:(NSData*)data { 148 { 149 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 150 XCTAssertThrows([input readInt32]); 151 } 152 { 153 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 154 XCTAssertThrows([input readInt64]); 155 } 156} 157 158- (void)testBytes { 159 NSData* data = bytes(0xa2, 0x74); 160 XCTAssertEqual(data.length, (NSUInteger)2); 161 XCTAssertEqual(((uint8_t*)data.bytes)[0], (uint8_t)0xa2); 162 XCTAssertEqual(((uint8_t*)data.bytes)[1], (uint8_t)0x74); 163} 164 165- (void)testReadBool { 166 { 167 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:bytes(0x00)]; 168 XCTAssertEqual(NO, [input readBool]); 169 } 170 { 171 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:bytes(0x01)]; 172 XCTAssertEqual(YES, [input readBool]); 173 } 174} 175 176- (void)testReadVarint { 177 [self assertReadVarint:bytes(0x00) value:0]; 178 [self assertReadVarint:bytes(0x01) value:1]; 179 [self assertReadVarint:bytes(0x7f) value:127]; 180 // 14882 181 [self assertReadVarint:bytes(0xa2, 0x74) value:(0x22 << 0) | (0x74 << 7)]; 182 // 1904930 183 [self assertReadVarint:bytes(0xa2, 0xa2, 0x74) value:(0x22 << 0) | (0x22 << 7) | (0x74 << 14)]; 184 // 243831074 185 [self assertReadVarint:bytes(0xa2, 0xa2, 0xa2, 0x74) 186 value:(0x22 << 0) | (0x22 << 7) | (0x22 << 14) | (0x74 << 21)]; 187 // 2961488830 188 [self assertReadVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b) 189 value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | (0x0bLL << 28)]; 190 191 // 64-bit 192 // 7256456126 193 [self assertReadVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b) 194 value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | (0x1bLL << 28)]; 195 // 41256202580718336 196 [self assertReadVarint:bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49) 197 value:(0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) | (0x43LL << 28) | 198 (0x49LL << 35) | (0x24LL << 42) | (0x49LL << 49)]; 199 // 11964378330978735131 200 [self assertReadVarint:bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01) 201 value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | (0x3bLL << 28) | 202 (0x56LL << 35) | (0x00LL << 42) | (0x05LL << 49) | (0x26LL << 56) | 203 (0x01ULL << 63)]; 204 205 // Failures 206 [self assertReadVarintFailure:bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 207 0x00)]; 208 [self assertReadVarintFailure:bytes(0x80)]; 209} 210 211- (void)testReadVarint32FromVarint64 { 212 { 213 // Turn on lower 31 bits of the upper half on a 64 bit varint. 214 NSData* data = bytes(0x80, 0x80, 0x80, 0x80, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0x7E); 215 216 int32_t value32 = 0x0; 217 GPBCodedInputStream* input32 = [GPBCodedInputStream streamWithData:data]; 218 XCTAssertEqual(value32, [input32 readInt32]); 219 220 int64_t value64 = INT64_MAX & 0xFFFFFFFF00000000; 221 GPBCodedInputStream* input64 = [GPBCodedInputStream streamWithData:data]; 222 XCTAssertEqual(value64, [input64 readInt64]); 223 } 224 { 225 // Turn on lower 31 bits and lower 31 bits on upper half on a 64 bit varint. 226 NSData* data = bytes(0xFF, 0xFF, 0xFF, 0xFF, 0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0x7E); 227 228 int32_t value32 = INT32_MAX; 229 GPBCodedInputStream* input32 = [GPBCodedInputStream streamWithData:data]; 230 XCTAssertEqual(value32, [input32 readInt32]); 231 232 int64_t value64 = INT64_MAX & 0xFFFFFFFF7FFFFFFF; 233 GPBCodedInputStream* input64 = [GPBCodedInputStream streamWithData:data]; 234 XCTAssertEqual(value64, [input64 readInt64]); 235 } 236 { 237 // Turn on bits 32 and 64 bit on a 64 bit varint. 238 NSData* data = bytes(0x80, 0x80, 0x80, 0x80, 0x88, 0x80, 0x80, 0x80, 0x80, 0x01); 239 240 int32_t value32 = INT32_MIN; 241 GPBCodedInputStream* input32 = [GPBCodedInputStream streamWithData:data]; 242 XCTAssertEqual(value32, [input32 readInt32]); 243 244 int64_t value64 = INT64_MIN | (0x01LL << 31); 245 GPBCodedInputStream* input64 = [GPBCodedInputStream streamWithData:data]; 246 XCTAssertEqual(value64, [input64 readInt64]); 247 } 248} 249 250- (void)testReadLittleEndian { 251 [self assertReadLittleEndian32:bytes(0x78, 0x56, 0x34, 0x12) value:0x12345678]; 252 [self assertReadLittleEndian32:bytes(0xf0, 0xde, 0xbc, 0x9a) value:0x9abcdef0]; 253 254 [self assertReadLittleEndian64:bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12) 255 value:0x123456789abcdef0LL]; 256 [self assertReadLittleEndian64:bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a) 257 value:0x9abcdef012345678LL]; 258} 259 260- (void)testReadWholeMessage { 261 TestAllTypes* message = [self allSetRepeatedCount:kGPBDefaultRepeatCount]; 262 263 NSData* rawBytes = message.data; 264 XCTAssertEqual(message.serializedSize, (size_t)rawBytes.length); 265 266 TestAllTypes* message2 = [TestAllTypes parseFromData:rawBytes extensionRegistry:nil error:NULL]; 267 [self assertAllFieldsSet:message2 repeatedCount:kGPBDefaultRepeatCount]; 268} 269 270- (void)testSkipWholeMessage { 271 TestAllTypes* message = [self allSetRepeatedCount:kGPBDefaultRepeatCount]; 272 NSData* rawBytes = message.data; 273 274 TestEmptyMessage* empty = [TestEmptyMessage parseFromData:rawBytes error:NULL]; 275 XCTAssertNotNil(empty); 276 GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] initFromMessage:empty] autorelease]; 277 NSMutableArray<NSNumber*>* fieldNumbers = [NSMutableArray arrayWithCapacity:ufs.count]; 278 for (GPBUnknownField* field in ufs) { 279 GPBWireFormat wireFormat; 280 switch (field.type) { 281 case GPBUnknownFieldTypeFixed32: 282 wireFormat = GPBWireFormatFixed32; 283 break; 284 case GPBUnknownFieldTypeFixed64: 285 wireFormat = GPBWireFormatFixed64; 286 break; 287 case GPBUnknownFieldTypeVarint: 288 wireFormat = GPBWireFormatVarint; 289 break; 290 case GPBUnknownFieldTypeLengthDelimited: 291 wireFormat = GPBWireFormatLengthDelimited; 292 break; 293 case GPBUnknownFieldTypeGroup: 294 wireFormat = GPBWireFormatStartGroup; 295 break; 296 case GPBUnknownFieldTypeLegacy: 297 XCTFail(@"Legacy field type not expected"); 298 wireFormat = GPBWireFormatVarint; 299 break; 300 } 301 uint32_t tag = GPBWireFormatMakeTag(field.number, wireFormat); 302 [fieldNumbers addObject:@(tag)]; 303 } 304 305 // Check the tags compared to what's in the UnknownFields to confirm the stream is 306 // skipping as expected (this covers the tags within a group also). 307 GPBCodedInputStream* input1 = [GPBCodedInputStream streamWithData:rawBytes]; 308 GPBCodedInputStream* input2 = [GPBCodedInputStream streamWithData:rawBytes]; 309#pragma clang diagnostic push 310#pragma clang diagnostic ignored "-Wdeprecated-declarations" 311 GPBUnknownFieldSet* unknownFields = [[[GPBUnknownFieldSet alloc] init] autorelease]; 312#pragma clang diagnostic pop 313 314 NSUInteger idx = 0; 315 while (YES) { 316 int32_t tag = [input1 readTag]; 317 XCTAssertEqual(tag, [input2 readTag]); 318 if (tag == 0) { 319 XCTAssertEqual(idx, fieldNumbers.count); 320 break; 321 } 322 XCTAssertEqual(tag, [fieldNumbers[idx] intValue]); 323 [unknownFields mergeFieldFrom:tag input:input1]; 324 [input2 skipField:tag]; 325 ++idx; 326 } 327} 328 329- (void)testLimit { 330 TestAllTypes* message = [self allSetRepeatedCount:kGPBDefaultRepeatCount]; 331 NSData* rawBytes = message.data; 332 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:rawBytes]; 333 XCTAssertEqual([input bytesUntilLimit], rawBytes.length); 334 [input pushLimit:8]; 335 XCTAssertEqual([input bytesUntilLimit], 8u); 336 [input popLimit:3]; 337 XCTAssertEqual([input bytesUntilLimit], 3u); 338 [input readTag]; 339 XCTAssertEqual([input position], 1u); 340 XCTAssertEqual([input bytesUntilLimit], 2u); 341} 342 343- (void)testReadHugeBlob { 344 // Allocate and initialize a 1MB blob. 345 NSMutableData* blob = [NSMutableData dataWithLength:1 << 20]; 346 for (NSUInteger i = 0; i < blob.length; i++) { 347 ((uint8_t*)blob.mutableBytes)[i] = (uint8_t)i; 348 } 349 350 // Make a message containing it. 351 TestAllTypes* message = [TestAllTypes message]; 352 [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; 353 [message setOptionalBytes:blob]; 354 355 // Serialize and parse it. Make sure to parse from an InputStream, not 356 // directly from a ByteString, so that CodedInputStream uses buffered 357 // reading. 358 NSData* messageData = message.data; 359 XCTAssertNotNil(messageData); 360 GPBCodedInputStream* stream = [GPBCodedInputStream streamWithData:messageData]; 361 TestAllTypes* message2 = [TestAllTypes parseFromCodedInputStream:stream 362 extensionRegistry:nil 363 error:NULL]; 364 365 XCTAssertEqualObjects(message.optionalBytes, message2.optionalBytes); 366 367 // Make sure all the other fields were parsed correctly. 368 TestAllTypes* message3 = [[message2 copy] autorelease]; 369 TestAllTypes* types = [self allSetRepeatedCount:kGPBDefaultRepeatCount]; 370 NSData* data = [types optionalBytes]; 371 [message3 setOptionalBytes:data]; 372 373 [self assertAllFieldsSet:message3 repeatedCount:kGPBDefaultRepeatCount]; 374} 375 376- (void)testReadMaliciouslyLargeBlob { 377 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 378 GPBCodedOutputStream* output = [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 379 380 int32_t tag = GPBWireFormatMakeTag(1, GPBWireFormatLengthDelimited); 381 [output writeRawVarint32:tag]; 382 [output writeRawVarint32:0x7FFFFFFF]; 383 uint8_t bytes[32] = {0}; 384 [output writeRawData:[NSData dataWithBytes:bytes length:32]]; 385 [output flush]; 386 387 NSData* data = [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 388 GPBCodedInputStream* input = 389 [GPBCodedInputStream streamWithData:[NSMutableData dataWithData:data]]; 390 XCTAssertEqual(tag, [input readTag]); 391 392 XCTAssertThrows([input readBytes]); 393} 394 395- (void)testReadEmptyString { 396 NSData* data = bytes(0x00); 397 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 398 XCTAssertEqualObjects(@"", [input readString]); 399} 400 401- (void)testInvalidGroupEndTagThrows { 402 NSData* data = bytes(0x0B, 0x1A, 0x02, 0x4B, 0x50, 0x14); 403 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 404 XCTAssertThrowsSpecificNamed([input skipMessage], NSException, GPBCodedInputStreamException, 405 @"should throw a GPBCodedInputStreamException exception "); 406} 407 408- (void)testBytesOver2GB { 409 NSData* data = bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x01, 0x02, 0x03); // don't need all the bytes 410 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 411 @try { 412 __unused NSData* result = [input readBytes]; 413 XCTFail(@"Should have thrown"); 414 } @catch (NSException* anException) { 415 // Ensure the correct error within the exception. 416 XCTAssertTrue([anException isKindOfClass:[NSException class]]); 417 XCTAssertEqualObjects(anException.name, GPBCodedInputStreamException); 418 NSDictionary* userInfo = anException.userInfo; 419 XCTAssertNotNil(userInfo); 420 NSError* err = userInfo[GPBCodedInputStreamUnderlyingErrorKey]; 421 XCTAssertNotNil(err); 422 XCTAssertEqualObjects(err.domain, GPBCodedInputStreamErrorDomain); 423 XCTAssertEqual(err.code, GPBCodedInputStreamErrorInvalidSize); 424 } 425} 426 427// Verifies fix for b/10315336. 428// Note: Now that there isn't a custom string class under the hood, this test 429// isn't as critical, but it does cover bad input and if a custom class is added 430// again, it will help validate that class' handing of bad utf8. 431- (void)testReadMalformedString { 432 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 433 GPBCodedOutputStream* output = [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 434 435 int32_t tag = 436 GPBWireFormatMakeTag(TestAllTypes_FieldNumber_DefaultString, GPBWireFormatLengthDelimited); 437 [output writeRawVarint32:tag]; 438 [output writeRawVarint32:5]; 439 // Create an invalid utf-8 byte array. 440 uint8_t bytes[] = {0xc2, 0xf2, 0x0, 0x0, 0x0}; 441 [output writeRawData:[NSData dataWithBytes:bytes length:sizeof(bytes)]]; 442 [output flush]; 443 444 NSData* data = [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 445 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 446 NSError* error = nil; 447 TestAllTypes* message = [TestAllTypes parseFromCodedInputStream:input 448 extensionRegistry:nil 449 error:&error]; 450 XCTAssertNotNil(error); 451 XCTAssertNil(message); 452} 453 454- (void)testBOMWithinStrings { 455 // We've seen servers that end up with BOMs within strings (not always at the 456 // start, and sometimes in multiple places), make sure they always parse 457 // correctly. (Again, this is inpart in case a custom string class is ever 458 // used again.) 459 const char* strs[] = { 460 "\xEF\xBB\xBF String with BOM", 461 "String with \xEF\xBB\xBF in middle", 462 "String with end bom \xEF\xBB\xBF", 463 "\xEF\xBB\xBF\xe2\x99\xa1", // BOM White Heart 464 "\xEF\xBB\xBF\xEF\xBB\xBF String with Two BOM", 465 }; 466 for (size_t i = 0; i < GPBARRAYSIZE(strs); ++i) { 467 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 468 GPBCodedOutputStream* output = [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 469 470 int32_t tag = 471 GPBWireFormatMakeTag(TestAllTypes_FieldNumber_DefaultString, GPBWireFormatLengthDelimited); 472 [output writeRawVarint32:tag]; 473 size_t length = strlen(strs[i]); 474 [output writeRawVarint32:(int32_t)length]; 475 [output writeRawData:[NSData dataWithBytes:strs[i] length:length]]; 476 [output flush]; 477 478 NSData* data = [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 479 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 480 TestAllTypes* message = [TestAllTypes parseFromCodedInputStream:input 481 extensionRegistry:nil 482 error:NULL]; 483 XCTAssertNotNil(message, @"Loop %zd", i); 484 // Ensure the string is there. NSString can consume the BOM in some 485 // cases, so don't actually check the string for exact equality. 486 XCTAssertTrue(message.defaultString.length > 0, @"Loop %zd", i); 487 } 488} 489 490- (void)assertReadByteToEndGroupFails:(NSData*)data { 491 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 492 uint32_t tag = [input readTag]; 493 XCTAssertThrows(GPBCodedInputStreamReadRetainedBytesToEndGroupNoCopy( 494 &input->state_, GPBWireFormatGetTagFieldNumber(tag))); 495} 496 497- (void)assertReadByteToEndGroup:(NSData*)data value:(NSData*)value { 498 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 499 uint32_t tag = [input readTag]; 500 NSData* readValue = GPBCodedInputStreamReadRetainedBytesToEndGroupNoCopy( 501 &input->state_, GPBWireFormatGetTagFieldNumber(tag)); 502 XCTAssertNotNil(readValue); 503 XCTAssertEqualObjects(readValue, value); 504 [readValue release]; 505} 506 507static NSData* DataForGroupsOfDepth(NSUInteger depth) { 508 NSMutableData* data = [NSMutableData dataWithCapacity:0]; 509 510 uint32_t byte = 35; // 35 = 0b100011 -> field 4/start group 511 for (NSUInteger i = 0; i < depth; ++i) { 512 [data appendBytes:&byte length:1]; 513 } 514 515 byte = 8; // 8 = 0b1000, -> field 1/varint 516 [data appendBytes:&byte length:1]; 517 byte = 1; // 1 -> varint value of 1 518 [data appendBytes:&byte length:1]; 519 520 byte = 36; // 36 = 0b100100 -> field 4/end group 521 for (NSUInteger i = 0; i < depth; ++i) { 522 [data appendBytes:&byte length:1]; 523 } 524 return data; 525} 526 527- (void)testBytesToEndGroup { 528 // 35 = 0b100011 -> field 4/start group 529 // 36 = 0b100100 -> field 4/end group 530 // 43 = 0b101011 -> field 5/end group 531 // 44 = 0b101100 -> field 5/end group 532 // 8 = 0b1000, 1 -> field 1/varint, value of 1 533 // 21 = 0b10101, 0x78, 0x56, 0x34, 0x12 -> field 2/fixed32, value of 0x12345678 534 // 25 = 0b11001, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12 -> field 3/fixed64, 535 // value of 0x123456789abcdef0LL 536 // 50 = 0b110010, 0x0 -> field 6/length delimited, length 0 537 // 50 = 0b110010, 0x1, 42 -> field 6/length delimited, length 1, byte 42 538 // 0 -> field 0 which is invalid/varint 539 // 15 = 0b1111 -> field 1, wire type 7 which is invalid 540 541 [self assertReadByteToEndGroup:bytes(35, 36) value:bytes(36)]; // empty group 542 [self assertReadByteToEndGroup:bytes(35, 8, 1, 36) value:bytes(8, 1, 36)]; // varint 543 [self assertReadByteToEndGroup:bytes(35, 21, 0x78, 0x56, 0x34, 0x12, 36) // fixed32 544 value:bytes(21, 0x78, 0x56, 0x34, 0x12, 36)]; 545 [self assertReadByteToEndGroup:bytes(35, 25, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, 546 36) // fixed64 547 value:bytes(25, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, 36)]; 548 [self assertReadByteToEndGroup:bytes(35, 50, 0, 36) 549 value:bytes(50, 0, 36)]; // length delimited, length 0 550 [self assertReadByteToEndGroup:bytes(35, 50, 1, 42, 36) 551 value:bytes(50, 1, 42, 36)]; // length delimited, length 1, byte 42 552 [self assertReadByteToEndGroup:bytes(35, 43, 44, 36) value:bytes(43, 44, 36)]; // Sub group 553 [self assertReadByteToEndGroup:bytes(35, 8, 1, 43, 8, 1, 44, 554 36) // varint and sub group with varint 555 value:bytes(8, 1, 43, 8, 1, 44, 36)]; 556 557 [self assertReadByteToEndGroupFails:bytes(35, 0, 36)]; // Invalid field number 558 [self assertReadByteToEndGroupFails:bytes(35, 15, 36)]; // Invalid wire type 559 [self assertReadByteToEndGroupFails:bytes(35, 21, 0x78, 0x56, 0x34)]; // truncated fixed32 560 [self assertReadByteToEndGroupFails:bytes(35, 25, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 561 0x34)]; // truncated fixed64 562 563 // Missing end group 564 [self assertReadByteToEndGroupFails:bytes(35)]; 565 [self assertReadByteToEndGroupFails:bytes(35, 8, 1)]; 566 [self assertReadByteToEndGroupFails:bytes(35, 43)]; 567 [self assertReadByteToEndGroupFails:bytes(35, 43, 8, 1)]; 568 569 // Wrong end group 570 [self assertReadByteToEndGroupFails:bytes(35, 44)]; 571 [self assertReadByteToEndGroupFails:bytes(35, 8, 1, 44)]; 572 [self assertReadByteToEndGroupFails:bytes(35, 43, 36)]; 573 [self assertReadByteToEndGroupFails:bytes(35, 43, 8, 1, 36)]; 574 [self assertReadByteToEndGroupFails:bytes(35, 43, 44, 44)]; 575 [self assertReadByteToEndGroupFails:bytes(35, 43, 8, 1, 44, 44)]; 576 577 // This is the same limit as within GPBCodedInputStream. 578 const NSUInteger kDefaultRecursionLimit = 100; 579 // That depth parses. 580 NSData* testData = DataForGroupsOfDepth(kDefaultRecursionLimit); 581 [self assertReadByteToEndGroup:testData 582 value:[testData subdataWithRange:NSMakeRange(1, testData.length - 1)]]; 583 // One more level deep fails. 584 testData = DataForGroupsOfDepth(kDefaultRecursionLimit + 1); 585 [self assertReadByteToEndGroupFails:testData]; 586} 587 588@end 589