1// Protocol Buffers - Google's data interchange format 2// Copyright 2008 Google Inc. All rights reserved. 3// https://developers.google.com/protocol-buffers/ 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions are 7// met: 8// 9// * Redistributions of source code must retain the above copyright 10// notice, this list of conditions and the following disclaimer. 11// * Redistributions in binary form must reproduce the above 12// copyright notice, this list of conditions and the following disclaimer 13// in the documentation and/or other materials provided with the 14// distribution. 15// * Neither the name of Google Inc. nor the names of its 16// contributors may be used to endorse or promote products derived from 17// this software without specific prior written permission. 18// 19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31#import "GPBTestUtilities.h" 32 33#import "GPBCodedInputStream.h" 34#import "GPBCodedOutputStream.h" 35#import "GPBUnknownFieldSet_PackagePrivate.h" 36#import "GPBUtilities_PackagePrivate.h" 37#import "google/protobuf/Unittest.pbobjc.h" 38 39@interface CodedInputStreamTests : GPBTestCase 40@end 41 42@implementation CodedInputStreamTests 43 44- (NSData*)bytes_with_sentinel:(int32_t)unused, ... { 45 va_list list; 46 va_start(list, unused); 47 48 NSMutableData* values = [NSMutableData dataWithCapacity:0]; 49 int32_t i; 50 51 while ((i = va_arg(list, int32_t)) != 256) { 52 NSAssert(i >= 0 && i < 256, @""); 53 uint8_t u = (uint8_t)i; 54 [values appendBytes:&u length:1]; 55 } 56 57 va_end(list); 58 59 return values; 60} 61 62#define bytes(...) [self bytes_with_sentinel:0, __VA_ARGS__, 256] 63 64- (void)testDecodeZigZag { 65 [self assertReadZigZag32:bytes(0x0) value:0]; 66 [self assertReadZigZag32:bytes(0x1) value:-1]; 67 [self assertReadZigZag32:bytes(0x2) value:1]; 68 [self assertReadZigZag32:bytes(0x3) value:-2]; 69 70 [self assertReadZigZag32:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0x07) value:(int32_t)0x3FFFFFFF]; 71 [self assertReadZigZag32:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x07) value:(int32_t)0xC0000000]; 72 [self assertReadZigZag32:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0x0F) value:(int32_t)0x7FFFFFFF]; 73 [self assertReadZigZag32:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x0F) value:(int32_t)0x80000000]; 74 75 [self assertReadZigZag64:bytes(0x0) value:0]; 76 [self assertReadZigZag64:bytes(0x1) value:-1]; 77 [self assertReadZigZag64:bytes(0x2) value:1]; 78 [self assertReadZigZag64:bytes(0x3) value:-2]; 79 80 [self assertReadZigZag64:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0x07) value:(int32_t)0x3FFFFFFF]; 81 [self assertReadZigZag64:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x07) value:(int32_t)0xC0000000]; 82 [self assertReadZigZag64:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0x0F) value:(int32_t)0x7FFFFFFF]; 83 [self assertReadZigZag64:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x0F) value:(int32_t)0x80000000]; 84 85 [self assertReadZigZag64:bytes(0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01) value:0x7FFFFFFFFFFFFFFFL]; 86 [self assertReadZigZag64:bytes(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01) value:0x8000000000000000L]; 87} 88 89- (void)assertReadVarint:(NSData*)data value:(int64_t)value { 90 if (value <= INT32_MAX && value >= INT32_MIN) { 91 { 92 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 93 XCTAssertEqual((int32_t)value, [input readInt32]); 94 } 95 { 96 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 97 XCTAssertEqual((int32_t)value, [input readEnum]); 98 } 99 } 100 if (value <= UINT32_MAX && value >= 0) { 101 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 102 XCTAssertEqual((uint32_t)value, [input readUInt32]); 103 } 104 { 105 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 106 XCTAssertEqual(value, [input readInt64]); 107 } 108 if (value >= 0) { 109 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 110 XCTAssertEqual((uint64_t)value, [input readUInt64]); 111 } 112} 113 114- (void)assertReadLittleEndian32:(NSData*)data value:(int32_t)value { 115 { 116 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 117 XCTAssertEqual(value, [input readSFixed32]); 118 } 119 { 120 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 121 XCTAssertEqual(GPBConvertInt32ToFloat(value), [input readFloat]); 122 } 123 { 124 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 125 XCTAssertEqual((uint32_t)value, [input readFixed32]); 126 } 127 { 128 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 129 XCTAssertEqual(value, [input readSFixed32]); 130 } 131} 132 133- (void)assertReadLittleEndian64:(NSData*)data value:(int64_t)value { 134 { 135 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 136 XCTAssertEqual(value, [input readSFixed64]); 137 } 138 { 139 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 140 XCTAssertEqual(GPBConvertInt64ToDouble(value), [input readDouble]); 141 } 142 { 143 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 144 XCTAssertEqual((uint64_t)value, [input readFixed64]); 145 } 146 { 147 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 148 XCTAssertEqual(value, [input readSFixed64]); 149 } 150} 151 152- (void)assertReadZigZag32:(NSData*)data value:(int64_t)value { 153 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 154 XCTAssertEqual((int32_t)value, [input readSInt32]); 155} 156 157- (void)assertReadZigZag64:(NSData*)data value:(int64_t)value { 158 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 159 XCTAssertEqual(value, [input readSInt64]); 160} 161 162- (void)assertReadVarintFailure:(NSData*)data { 163 { 164 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 165 XCTAssertThrows([input readInt32]); 166 } 167 { 168 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 169 XCTAssertThrows([input readInt64]); 170 } 171} 172 173- (void)testBytes { 174 NSData* data = bytes(0xa2, 0x74); 175 XCTAssertEqual(data.length, (NSUInteger)2); 176 XCTAssertEqual(((uint8_t*)data.bytes)[0], (uint8_t)0xa2); 177 XCTAssertEqual(((uint8_t*)data.bytes)[1], (uint8_t)0x74); 178} 179 180- (void)testReadBool { 181 { 182 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:bytes(0x00)]; 183 XCTAssertEqual(NO, [input readBool]); 184 } 185 { 186 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:bytes(0x01)]; 187 XCTAssertEqual(YES, [input readBool]); 188 } 189} 190 191- (void)testReadVarint { 192 [self assertReadVarint:bytes(0x00) value:0]; 193 [self assertReadVarint:bytes(0x01) value:1]; 194 [self assertReadVarint:bytes(0x7f) value:127]; 195 // 14882 196 [self assertReadVarint:bytes(0xa2, 0x74) value:(0x22 << 0) | (0x74 << 7)]; 197 // 1904930 198 [self assertReadVarint:bytes(0xa2, 0xa2, 0x74) value:(0x22 << 0) | (0x22 << 7) | (0x74 << 14)]; 199 // 243831074 200 [self assertReadVarint:bytes(0xa2, 0xa2, 0xa2, 0x74) 201 value:(0x22 << 0) | (0x22 << 7) | (0x22 << 14) | (0x74 << 21)]; 202 // 2961488830 203 [self assertReadVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b) 204 value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) | 205 (0x04 << 21) | (0x0bLL << 28)]; 206 207 // 64-bit 208 // 7256456126 209 [self assertReadVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b) 210 value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) | 211 (0x04 << 21) | (0x1bLL << 28)]; 212 // 41256202580718336 213 [self assertReadVarint:bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49) 214 value:(0x00 << 0) | (0x66 << 7) | (0x6b << 14) | 215 (0x1c << 21) | (0x43LL << 28) | (0x49LL << 35) | 216 (0x24LL << 42) | (0x49LL << 49)]; 217 // 11964378330978735131 218 [self 219 assertReadVarint:bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 220 0xa6, 0x01) 221 value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | 222 (0x3bLL << 28) | (0x56LL << 35) | (0x00LL << 42) | 223 (0x05LL << 49) | (0x26LL << 56) | (0x01ULL << 63)]; 224 225 // Failures 226 [self assertReadVarintFailure:bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 227 0x80, 0x80, 0x80, 0x00)]; 228 [self assertReadVarintFailure:bytes(0x80)]; 229} 230 231- (void)testReadVarint32FromVarint64 { 232 { 233 // Turn on lower 31 bits of the upper half on a 64 bit varint. 234 NSData* data = bytes(0x80, 0x80, 0x80, 0x80, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0x7E); 235 236 int32_t value32 = 0x0; 237 GPBCodedInputStream* input32 = [GPBCodedInputStream streamWithData:data]; 238 XCTAssertEqual(value32, [input32 readInt32]); 239 240 int64_t value64 = INT64_MAX & 0xFFFFFFFF00000000; 241 GPBCodedInputStream* input64 = [GPBCodedInputStream streamWithData:data]; 242 XCTAssertEqual(value64, [input64 readInt64]); 243 } 244 { 245 // Turn on lower 31 bits and lower 31 bits on upper half on a 64 bit varint. 246 NSData* data = bytes(0xFF, 0xFF, 0xFF, 0xFF, 0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0x7E); 247 248 int32_t value32 = INT32_MAX; 249 GPBCodedInputStream* input32 = [GPBCodedInputStream streamWithData:data]; 250 XCTAssertEqual(value32, [input32 readInt32]); 251 252 int64_t value64 = INT64_MAX & 0xFFFFFFFF7FFFFFFF; 253 GPBCodedInputStream* input64 = [GPBCodedInputStream streamWithData:data]; 254 XCTAssertEqual(value64, [input64 readInt64]); 255 } 256 { 257 // Turn on bits 32 and 64 bit on a 64 bit varint. 258 NSData* data = bytes(0x80, 0x80, 0x80, 0x80, 0x88, 0x80, 0x80, 0x80, 0x80, 0x01); 259 260 int32_t value32 = INT32_MIN; 261 GPBCodedInputStream* input32 = [GPBCodedInputStream streamWithData:data]; 262 XCTAssertEqual(value32, [input32 readInt32]); 263 264 int64_t value64 = INT64_MIN | (0x01LL << 31); 265 GPBCodedInputStream* input64 = [GPBCodedInputStream streamWithData:data]; 266 XCTAssertEqual(value64, [input64 readInt64]); 267 } 268} 269 270- (void)testReadLittleEndian { 271 [self assertReadLittleEndian32:bytes(0x78, 0x56, 0x34, 0x12) 272 value:0x12345678]; 273 [self assertReadLittleEndian32:bytes(0xf0, 0xde, 0xbc, 0x9a) 274 value:0x9abcdef0]; 275 276 [self assertReadLittleEndian64:bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 277 0x12) 278 value:0x123456789abcdef0LL]; 279 [self assertReadLittleEndian64:bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 280 0x9a) 281 value:0x9abcdef012345678LL]; 282} 283 284- (void)testReadWholeMessage { 285 TestAllTypes* message = [self allSetRepeatedCount:kGPBDefaultRepeatCount]; 286 287 NSData* rawBytes = message.data; 288 XCTAssertEqual(message.serializedSize, (size_t)rawBytes.length); 289 290 TestAllTypes* message2 = 291 [TestAllTypes parseFromData:rawBytes extensionRegistry:nil error:NULL]; 292 [self assertAllFieldsSet:message2 repeatedCount:kGPBDefaultRepeatCount]; 293} 294 295- (void)testSkipWholeMessage { 296 TestAllTypes* message = [self allSetRepeatedCount:kGPBDefaultRepeatCount]; 297 NSData* rawBytes = message.data; 298 299 // Create two parallel inputs. Parse one as unknown fields while using 300 // skipField() to skip each field on the other. Expect the same tags. 301 GPBCodedInputStream* input1 = [GPBCodedInputStream streamWithData:rawBytes]; 302 GPBCodedInputStream* input2 = [GPBCodedInputStream streamWithData:rawBytes]; 303 GPBUnknownFieldSet* unknownFields = 304 [[[GPBUnknownFieldSet alloc] init] autorelease]; 305 306 while (YES) { 307 int32_t tag = [input1 readTag]; 308 XCTAssertEqual(tag, [input2 readTag]); 309 if (tag == 0) { 310 break; 311 } 312 [unknownFields mergeFieldFrom:tag input:input1]; 313 [input2 skipField:tag]; 314 } 315} 316 317- (void)testReadHugeBlob { 318 // Allocate and initialize a 1MB blob. 319 NSMutableData* blob = [NSMutableData dataWithLength:1 << 20]; 320 for (NSUInteger i = 0; i < blob.length; i++) { 321 ((uint8_t*)blob.mutableBytes)[i] = (uint8_t)i; 322 } 323 324 // Make a message containing it. 325 TestAllTypes* message = [TestAllTypes message]; 326 [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; 327 [message setOptionalBytes:blob]; 328 329 // Serialize and parse it. Make sure to parse from an InputStream, not 330 // directly from a ByteString, so that CodedInputStream uses buffered 331 // reading. 332 NSData *messageData = message.data; 333 XCTAssertNotNil(messageData); 334 GPBCodedInputStream* stream = 335 [GPBCodedInputStream streamWithData:messageData]; 336 TestAllTypes* message2 = [TestAllTypes parseFromCodedInputStream:stream 337 extensionRegistry:nil 338 error:NULL]; 339 340 XCTAssertEqualObjects(message.optionalBytes, message2.optionalBytes); 341 342 // Make sure all the other fields were parsed correctly. 343 TestAllTypes* message3 = [[message2 copy] autorelease]; 344 TestAllTypes* types = [self allSetRepeatedCount:kGPBDefaultRepeatCount]; 345 NSData* data = [types optionalBytes]; 346 [message3 setOptionalBytes:data]; 347 348 [self assertAllFieldsSet:message3 repeatedCount:kGPBDefaultRepeatCount]; 349} 350 351- (void)testReadMaliciouslyLargeBlob { 352 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 353 GPBCodedOutputStream* output = 354 [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 355 356 int32_t tag = GPBWireFormatMakeTag(1, GPBWireFormatLengthDelimited); 357 [output writeRawVarint32:tag]; 358 [output writeRawVarint32:0x7FFFFFFF]; 359 uint8_t bytes[32] = {0}; 360 [output writeRawData:[NSData dataWithBytes:bytes length:32]]; 361 [output flush]; 362 363 NSData* data = 364 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 365 GPBCodedInputStream* input = 366 [GPBCodedInputStream streamWithData:[NSMutableData dataWithData:data]]; 367 XCTAssertEqual(tag, [input readTag]); 368 369 XCTAssertThrows([input readBytes]); 370} 371 372- (void)testReadEmptyString { 373 NSData *data = bytes(0x00); 374 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 375 XCTAssertEqualObjects(@"", [input readString]); 376} 377 378- (void)testInvalidGroupEndTagThrows { 379 NSData *data = bytes(0x0B, 0x1A, 0x02, 0x4B, 0x50, 0x14); 380 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 381 XCTAssertThrowsSpecificNamed([input skipMessage], 382 NSException, 383 GPBCodedInputStreamException, 384 @"should throw a GPBCodedInputStreamException exception "); 385} 386 387- (void)testBytesWithNegativeSize { 388 NSData *data = bytes(0xFF, 0xFF, 0xFF, 0xFF, 0x0F); 389 GPBCodedInputStream *input = [GPBCodedInputStream streamWithData:data]; 390 XCTAssertNil([input readBytes]); 391} 392 393// Verifies fix for b/10315336. 394// Note: Now that there isn't a custom string class under the hood, this test 395// isn't as critical, but it does cover bad input and if a custom class is added 396// again, it will help validate that class' handing of bad utf8. 397- (void)testReadMalformedString { 398 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 399 GPBCodedOutputStream* output = 400 [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 401 402 int32_t tag = GPBWireFormatMakeTag(TestAllTypes_FieldNumber_DefaultString, 403 GPBWireFormatLengthDelimited); 404 [output writeRawVarint32:tag]; 405 [output writeRawVarint32:5]; 406 // Create an invalid utf-8 byte array. 407 uint8_t bytes[] = {0xc2, 0xf2, 0x0, 0x0, 0x0}; 408 [output writeRawData:[NSData dataWithBytes:bytes length:sizeof(bytes)]]; 409 [output flush]; 410 411 NSData *data = 412 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 413 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 414 NSError *error = nil; 415 TestAllTypes* message = [TestAllTypes parseFromCodedInputStream:input 416 extensionRegistry:nil 417 error:&error]; 418 XCTAssertNotNil(error); 419 XCTAssertNil(message); 420} 421 422- (void)testBOMWithinStrings { 423 // We've seen servers that end up with BOMs within strings (not always at the 424 // start, and sometimes in multiple places), make sure they always parse 425 // correctly. (Again, this is inpart incase a custom string class is ever 426 // used again.) 427 const char* strs[] = { 428 "\xEF\xBB\xBF String with BOM", 429 "String with \xEF\xBB\xBF in middle", 430 "String with end bom \xEF\xBB\xBF", 431 "\xEF\xBB\xBF\xe2\x99\xa1", // BOM White Heart 432 "\xEF\xBB\xBF\xEF\xBB\xBF String with Two BOM", 433 }; 434 for (size_t i = 0; i < GPBARRAYSIZE(strs); ++i) { 435 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 436 GPBCodedOutputStream* output = 437 [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 438 439 int32_t tag = GPBWireFormatMakeTag(TestAllTypes_FieldNumber_DefaultString, 440 GPBWireFormatLengthDelimited); 441 [output writeRawVarint32:tag]; 442 size_t length = strlen(strs[i]); 443 [output writeRawVarint32:(int32_t)length]; 444 [output writeRawData:[NSData dataWithBytes:strs[i] length:length]]; 445 [output flush]; 446 447 NSData* data = 448 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 449 GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data]; 450 TestAllTypes* message = [TestAllTypes parseFromCodedInputStream:input 451 extensionRegistry:nil 452 error:NULL]; 453 XCTAssertNotNil(message, @"Loop %zd", i); 454 // Ensure the string is there. NSString can consume the BOM in some 455 // cases, so don't actually check the string for exact equality. 456 XCTAssertTrue(message.defaultString.length > 0, @"Loop %zd", i); 457 } 458} 459 460@end 461