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 "GPBCodedOutputStream_PackagePrivate.h" 34#import "GPBCodedInputStream.h" 35#import "GPBUtilities_PackagePrivate.h" 36#import "google/protobuf/Unittest.pbobjc.h" 37 38@interface GPBCodedOutputStream (InternalMethods) 39// Declared in the .m file, expose for testing. 40- (instancetype)initWithOutputStream:(NSOutputStream *)output 41 data:(NSMutableData *)data; 42@end 43 44@interface GPBCodedOutputStream (Helper) 45+ (instancetype)streamWithOutputStream:(NSOutputStream *)output 46 bufferSize:(size_t)bufferSize; 47@end 48 49@implementation GPBCodedOutputStream (Helper) 50+ (instancetype)streamWithOutputStream:(NSOutputStream *)output 51 bufferSize:(size_t)bufferSize { 52 NSMutableData *data = [NSMutableData dataWithLength:bufferSize]; 53 return [[[self alloc] initWithOutputStream:output data:data] autorelease]; 54} 55@end 56 57@interface CodedOutputStreamTests : GPBTestCase 58@end 59 60@implementation CodedOutputStreamTests 61 62- (NSData*)bytes_with_sentinel:(int32_t)unused, ... { 63 va_list list; 64 va_start(list, unused); 65 66 NSMutableData* values = [NSMutableData dataWithCapacity:0]; 67 int32_t i; 68 69 while ((i = va_arg(list, int32_t)) != 256) { 70 NSAssert(i >= 0 && i < 256, @""); 71 uint8_t u = (uint8_t)i; 72 [values appendBytes:&u length:1]; 73 } 74 75 va_end(list); 76 77 return values; 78} 79 80#define bytes(...) [self bytes_with_sentinel:0, __VA_ARGS__, 256] 81 82- (void)assertWriteLittleEndian32:(NSData*)data value:(int32_t)value { 83 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 84 GPBCodedOutputStream* output = 85 [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 86 [output writeRawLittleEndian32:(int32_t)value]; 87 [output flush]; 88 89 NSData* actual = 90 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 91 XCTAssertEqualObjects(data, actual); 92 93 // Try different block sizes. 94 for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { 95 rawOutput = [NSOutputStream outputStreamToMemory]; 96 output = [GPBCodedOutputStream streamWithOutputStream:rawOutput 97 bufferSize:blockSize]; 98 [output writeRawLittleEndian32:(int32_t)value]; 99 [output flush]; 100 101 actual = [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 102 XCTAssertEqualObjects(data, actual); 103 } 104} 105 106- (void)assertWriteLittleEndian64:(NSData*)data value:(int64_t)value { 107 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 108 GPBCodedOutputStream* output = 109 [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 110 [output writeRawLittleEndian64:value]; 111 [output flush]; 112 113 NSData* actual = 114 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 115 XCTAssertEqualObjects(data, actual); 116 117 // Try different block sizes. 118 for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { 119 rawOutput = [NSOutputStream outputStreamToMemory]; 120 output = [GPBCodedOutputStream streamWithOutputStream:rawOutput 121 bufferSize:blockSize]; 122 [output writeRawLittleEndian64:value]; 123 [output flush]; 124 125 actual = [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 126 XCTAssertEqualObjects(data, actual); 127 } 128} 129 130- (void)assertWriteVarint:(NSData*)data value:(int64_t)value { 131 // Only do 32-bit write if the value fits in 32 bits. 132 if (GPBLogicalRightShift64(value, 32) == 0) { 133 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 134 GPBCodedOutputStream* output = 135 [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 136 [output writeRawVarint32:(int32_t)value]; 137 [output flush]; 138 139 NSData* actual = 140 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 141 XCTAssertEqualObjects(data, actual); 142 143 // Also try computing size. 144 XCTAssertEqual(GPBComputeRawVarint32Size((int32_t)value), 145 (size_t)data.length); 146 } 147 148 { 149 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 150 GPBCodedOutputStream* output = 151 [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 152 [output writeRawVarint64:value]; 153 [output flush]; 154 155 NSData* actual = 156 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 157 XCTAssertEqualObjects(data, actual); 158 159 // Also try computing size. 160 XCTAssertEqual(GPBComputeRawVarint64Size(value), (size_t)data.length); 161 } 162 163 // Try different block sizes. 164 for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { 165 // Only do 32-bit write if the value fits in 32 bits. 166 if (GPBLogicalRightShift64(value, 32) == 0) { 167 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 168 GPBCodedOutputStream* output = 169 [GPBCodedOutputStream streamWithOutputStream:rawOutput 170 bufferSize:blockSize]; 171 172 [output writeRawVarint32:(int32_t)value]; 173 [output flush]; 174 175 NSData* actual = 176 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 177 XCTAssertEqualObjects(data, actual); 178 } 179 180 { 181 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 182 GPBCodedOutputStream* output = 183 [GPBCodedOutputStream streamWithOutputStream:rawOutput 184 bufferSize:blockSize]; 185 186 [output writeRawVarint64:value]; 187 [output flush]; 188 189 NSData* actual = 190 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 191 XCTAssertEqualObjects(data, actual); 192 } 193 } 194} 195 196- (void)assertWriteStringNoTag:(NSData*)data 197 value:(NSString *)value 198 context:(NSString *)contextMessage { 199 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 200 GPBCodedOutputStream* output = 201 [GPBCodedOutputStream streamWithOutputStream:rawOutput]; 202 [output writeStringNoTag:value]; 203 [output flush]; 204 205 NSData* actual = 206 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 207 XCTAssertEqualObjects(data, actual, @"%@", contextMessage); 208 209 // Try different block sizes. 210 for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { 211 rawOutput = [NSOutputStream outputStreamToMemory]; 212 output = [GPBCodedOutputStream streamWithOutputStream:rawOutput 213 bufferSize:blockSize]; 214 [output writeStringNoTag:value]; 215 [output flush]; 216 217 actual = [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 218 XCTAssertEqualObjects(data, actual, @"%@", contextMessage); 219 } 220} 221 222- (void)testWriteVarint1 { 223 [self assertWriteVarint:bytes(0x00) value:0]; 224} 225 226- (void)testWriteVarint2 { 227 [self assertWriteVarint:bytes(0x01) value:1]; 228} 229 230- (void)testWriteVarint3 { 231 [self assertWriteVarint:bytes(0x7f) value:127]; 232} 233 234- (void)testWriteVarint4 { 235 // 14882 236 [self assertWriteVarint:bytes(0xa2, 0x74) value:(0x22 << 0) | (0x74 << 7)]; 237} 238 239- (void)testWriteVarint5 { 240 // The sign/nosign distinction is done here because normally varints are 241 // around 64bit values, but for some cases a 32bit value is forced with 242 // with the sign bit (tags, uint32, etc.) 243 244 // 1887747006 (no sign bit) 245 [self assertWriteVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x07) 246 value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) | 247 (0x04 << 21) | (0x07LL << 28)]; 248 // 2961488830 (sign bit) 249 [self assertWriteVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b) 250 value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) | 251 (0x04 << 21) | (0x0bLL << 28)]; 252} 253 254- (void)testWriteVarint6 { 255 // 64-bit 256 // 7256456126 257 [self assertWriteVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b) 258 value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) | 259 (0x04 << 21) | (0x1bLL << 28)]; 260} 261 262- (void)testWriteVarint7 { 263 // 41256202580718336 264 [self assertWriteVarint:bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49) 265 value:(0x00 << 0) | (0x66 << 7) | (0x6b << 14) | 266 (0x1c << 21) | (0x43LL << 28) | (0x49LL << 35) | 267 (0x24LL << 42) | (0x49LL << 49)]; 268} 269 270- (void)testWriteVarint8 { 271 // 11964378330978735131 272 [self assertWriteVarint:bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 273 0xa6, 0x01) 274 value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | 275 (0x42 << 21) | (0x3bLL << 28) | (0x56LL << 35) | 276 (0x00LL << 42) | (0x05LL << 49) | (0x26LL << 56) | 277 (0x01ULL << 63)]; 278} 279 280- (void)testWriteLittleEndian { 281 [self assertWriteLittleEndian32:bytes(0x78, 0x56, 0x34, 0x12) 282 value:0x12345678]; 283 [self assertWriteLittleEndian32:bytes(0xf0, 0xde, 0xbc, 0x9a) 284 value:0x9abcdef0]; 285 286 [self assertWriteLittleEndian64:bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 287 0x34, 0x12) 288 value:0x123456789abcdef0LL]; 289 [self assertWriteLittleEndian64:bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 290 0xbc, 0x9a) 291 value:0x9abcdef012345678LL]; 292} 293 294- (void)testEncodeZigZag { 295 XCTAssertEqual(0U, GPBEncodeZigZag32(0)); 296 XCTAssertEqual(1U, GPBEncodeZigZag32(-1)); 297 XCTAssertEqual(2U, GPBEncodeZigZag32(1)); 298 XCTAssertEqual(3U, GPBEncodeZigZag32(-2)); 299 XCTAssertEqual(0x7FFFFFFEU, GPBEncodeZigZag32(0x3FFFFFFF)); 300 XCTAssertEqual(0x7FFFFFFFU, GPBEncodeZigZag32(0xC0000000)); 301 XCTAssertEqual(0xFFFFFFFEU, GPBEncodeZigZag32(0x7FFFFFFF)); 302 XCTAssertEqual(0xFFFFFFFFU, GPBEncodeZigZag32(0x80000000)); 303 304 XCTAssertEqual(0ULL, GPBEncodeZigZag64(0)); 305 XCTAssertEqual(1ULL, GPBEncodeZigZag64(-1)); 306 XCTAssertEqual(2ULL, GPBEncodeZigZag64(1)); 307 XCTAssertEqual(3ULL, GPBEncodeZigZag64(-2)); 308 XCTAssertEqual(0x000000007FFFFFFEULL, 309 GPBEncodeZigZag64(0x000000003FFFFFFFLL)); 310 XCTAssertEqual(0x000000007FFFFFFFULL, 311 GPBEncodeZigZag64(0xFFFFFFFFC0000000LL)); 312 XCTAssertEqual(0x00000000FFFFFFFEULL, 313 GPBEncodeZigZag64(0x000000007FFFFFFFLL)); 314 XCTAssertEqual(0x00000000FFFFFFFFULL, 315 GPBEncodeZigZag64(0xFFFFFFFF80000000LL)); 316 XCTAssertEqual(0xFFFFFFFFFFFFFFFEULL, 317 GPBEncodeZigZag64(0x7FFFFFFFFFFFFFFFLL)); 318 XCTAssertEqual(0xFFFFFFFFFFFFFFFFULL, 319 GPBEncodeZigZag64(0x8000000000000000LL)); 320 321 // Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1) 322 // were chosen semi-randomly via keyboard bashing. 323 XCTAssertEqual(0U, GPBEncodeZigZag32(GPBDecodeZigZag32(0))); 324 XCTAssertEqual(1U, GPBEncodeZigZag32(GPBDecodeZigZag32(1))); 325 XCTAssertEqual(-1U, GPBEncodeZigZag32(GPBDecodeZigZag32(-1))); 326 XCTAssertEqual(14927U, GPBEncodeZigZag32(GPBDecodeZigZag32(14927))); 327 XCTAssertEqual(-3612U, GPBEncodeZigZag32(GPBDecodeZigZag32(-3612))); 328 329 XCTAssertEqual(0ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(0))); 330 XCTAssertEqual(1ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(1))); 331 XCTAssertEqual(-1ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(-1))); 332 XCTAssertEqual(14927ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(14927))); 333 XCTAssertEqual(-3612ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(-3612))); 334 335 XCTAssertEqual(856912304801416ULL, 336 GPBEncodeZigZag64(GPBDecodeZigZag64(856912304801416LL))); 337 XCTAssertEqual(-75123905439571256ULL, 338 GPBEncodeZigZag64(GPBDecodeZigZag64(-75123905439571256LL))); 339} 340 341- (void)testWriteWholeMessage { 342 // Not kGPBDefaultRepeatCount because we are comparing to a golden master file 343 // that was generated with 2. 344 TestAllTypes* message = [self allSetRepeatedCount:2]; 345 346 NSData* rawBytes = message.data; 347 NSData* goldenData = 348 [self getDataFileNamed:@"golden_message" dataToWrite:rawBytes]; 349 XCTAssertEqualObjects(rawBytes, goldenData); 350 351 // Try different block sizes. 352 for (int blockSize = 1; blockSize < 256; blockSize *= 2) { 353 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory]; 354 GPBCodedOutputStream* output = 355 [GPBCodedOutputStream streamWithOutputStream:rawOutput 356 bufferSize:blockSize]; 357 [message writeToCodedOutputStream:output]; 358 [output flush]; 359 360 NSData* actual = 361 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 362 XCTAssertEqualObjects(rawBytes, actual); 363 } 364 365 // Not kGPBDefaultRepeatCount because we are comparing to a golden master file 366 // that was generated with 2. 367 TestAllExtensions* extensions = [self allExtensionsSetRepeatedCount:2]; 368 rawBytes = extensions.data; 369 goldenData = [self getDataFileNamed:@"golden_packed_fields_message" 370 dataToWrite:rawBytes]; 371 XCTAssertEqualObjects(rawBytes, goldenData); 372} 373 374- (void)testCFStringGetCStringPtrAndStringsWithNullChars { 375 // This test exists to verify that CFStrings with embedded NULLs still expose 376 // their raw buffer if they are backed by UTF8 storage. If this fails, the 377 // quick/direct access paths in GPBCodedOutputStream that depend on 378 // CFStringGetCStringPtr need to be re-evalutated (maybe just removed). 379 // And yes, we do get NULLs in strings from some servers. 380 381 char zeroTest[] = "\0Test\0String"; 382 // Note: there is a \0 at the end of this since it is a c-string. 383 NSString *asNSString = [[NSString alloc] initWithBytes:zeroTest 384 length:sizeof(zeroTest) 385 encoding:NSUTF8StringEncoding]; 386 const char *cString = 387 CFStringGetCStringPtr((CFStringRef)asNSString, kCFStringEncodingUTF8); 388 XCTAssertTrue(cString != NULL); 389 // Again, if the above assert fails, then it means NSString no longer exposes 390 // the raw utf8 storage of a string created from utf8 input, so the code using 391 // CFStringGetCStringPtr in GPBCodedOutputStream will still work (it will take 392 // a different code path); but the optimizations for when 393 // CFStringGetCStringPtr does work could possibly go away. 394 395 XCTAssertEqual(sizeof(zeroTest), 396 [asNSString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); 397 XCTAssertTrue(0 == memcmp(cString, zeroTest, sizeof(zeroTest))); 398 [asNSString release]; 399} 400 401- (void)testWriteStringsWithZeroChar { 402 // Unicode allows `\0` as a character, and NSString is a class cluster, so 403 // there are a few different classes that could end up beind a given string. 404 // Historically, we've seen differences based on constant strings in code and 405 // strings built via the NSString apis. So this round trips them to ensure 406 // they are acting as expected. 407 408 NSArray<NSString *> *strs = @[ 409 @"\0at start", 410 @"in\0middle", 411 @"at end\0", 412 ]; 413 int i = 0; 414 for (NSString *str in strs) { 415 NSData *asUTF8 = [str dataUsingEncoding:NSUTF8StringEncoding]; 416 NSMutableData *expected = [NSMutableData data]; 417 uint8_t lengthByte = (uint8_t)asUTF8.length; 418 [expected appendBytes:&lengthByte length:1]; 419 [expected appendData:asUTF8]; 420 421 NSString *context = [NSString stringWithFormat:@"Loop %d - Literal", i]; 422 [self assertWriteStringNoTag:expected value:str context:context]; 423 424 // Force a new string to be built which gets a different class from the 425 // NSString class cluster than the literal did. 426 NSString *str2 = [NSString stringWithFormat:@"%@", str]; 427 context = [NSString stringWithFormat:@"Loop %d - Built", i]; 428 [self assertWriteStringNoTag:expected value:str2 context:context]; 429 430 ++i; 431 } 432} 433 434- (void)testThatItThrowsWhenWriteRawPtrFails { 435 NSOutputStream *output = [NSOutputStream outputStreamToMemory]; 436 GPBCodedOutputStream *codedOutput = 437 [GPBCodedOutputStream streamWithOutputStream:output bufferSize:0]; // Skip buffering. 438 [output close]; // Close the output stream to force failure on write. 439 const char *cString = "raw"; 440 XCTAssertThrowsSpecificNamed([codedOutput writeRawPtr:cString offset:0 length:strlen(cString)], 441 NSException, GPBCodedOutputStreamException_WriteFailed); 442} 443 444@end 445