blob: 109239d5f31d550f8128870777f71a3220cd5dce [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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
Austin Schuh40c16522018-10-28 20:27:54 -070033#import "GPBCodedOutputStream_PackagePrivate.h"
Brian Silverman9c614bc2016-02-15 20:20:02 -050034#import "GPBCodedInputStream.h"
35#import "GPBUtilities_PackagePrivate.h"
36#import "google/protobuf/Unittest.pbobjc.h"
37
Austin Schuh40c16522018-10-28 20:27:54 -070038@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
Brian Silverman9c614bc2016-02-15 20:20:02 -050057@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
Austin Schuh40c16522018-10-28 20:27:54 -0700196- (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
Brian Silverman9c614bc2016-02-15 20:20:02 -0500222- (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 // 2961488830
241 [self assertWriteVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b)
242 value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) |
243 (0x04 << 21) | (0x0bLL << 28)];
244}
245
246- (void)testWriteVarint6 {
247 // 64-bit
248 // 7256456126
249 [self assertWriteVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b)
250 value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) |
251 (0x04 << 21) | (0x1bLL << 28)];
252}
253
254- (void)testWriteVarint7 {
255 // 41256202580718336
256 [self assertWriteVarint:bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49)
257 value:(0x00 << 0) | (0x66 << 7) | (0x6b << 14) |
258 (0x1c << 21) | (0x43LL << 28) | (0x49LL << 35) |
259 (0x24LL << 42) | (0x49LL << 49)];
260}
261
262- (void)testWriteVarint8 {
263 // 11964378330978735131
264 [self assertWriteVarint:bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85,
265 0xa6, 0x01)
266 value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) |
267 (0x42 << 21) | (0x3bLL << 28) | (0x56LL << 35) |
268 (0x00LL << 42) | (0x05LL << 49) | (0x26LL << 56) |
Austin Schuh40c16522018-10-28 20:27:54 -0700269 (0x01ULL << 63)];
Brian Silverman9c614bc2016-02-15 20:20:02 -0500270}
271
272- (void)testWriteLittleEndian {
273 [self assertWriteLittleEndian32:bytes(0x78, 0x56, 0x34, 0x12)
274 value:0x12345678];
275 [self assertWriteLittleEndian32:bytes(0xf0, 0xde, 0xbc, 0x9a)
276 value:0x9abcdef0];
277
278 [self assertWriteLittleEndian64:bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56,
279 0x34, 0x12)
280 value:0x123456789abcdef0LL];
281 [self assertWriteLittleEndian64:bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde,
282 0xbc, 0x9a)
283 value:0x9abcdef012345678LL];
284}
285
286- (void)testEncodeZigZag {
287 XCTAssertEqual(0U, GPBEncodeZigZag32(0));
288 XCTAssertEqual(1U, GPBEncodeZigZag32(-1));
289 XCTAssertEqual(2U, GPBEncodeZigZag32(1));
290 XCTAssertEqual(3U, GPBEncodeZigZag32(-2));
291 XCTAssertEqual(0x7FFFFFFEU, GPBEncodeZigZag32(0x3FFFFFFF));
292 XCTAssertEqual(0x7FFFFFFFU, GPBEncodeZigZag32(0xC0000000));
293 XCTAssertEqual(0xFFFFFFFEU, GPBEncodeZigZag32(0x7FFFFFFF));
294 XCTAssertEqual(0xFFFFFFFFU, GPBEncodeZigZag32(0x80000000));
295
296 XCTAssertEqual(0ULL, GPBEncodeZigZag64(0));
297 XCTAssertEqual(1ULL, GPBEncodeZigZag64(-1));
298 XCTAssertEqual(2ULL, GPBEncodeZigZag64(1));
299 XCTAssertEqual(3ULL, GPBEncodeZigZag64(-2));
300 XCTAssertEqual(0x000000007FFFFFFEULL,
301 GPBEncodeZigZag64(0x000000003FFFFFFFLL));
302 XCTAssertEqual(0x000000007FFFFFFFULL,
303 GPBEncodeZigZag64(0xFFFFFFFFC0000000LL));
304 XCTAssertEqual(0x00000000FFFFFFFEULL,
305 GPBEncodeZigZag64(0x000000007FFFFFFFLL));
306 XCTAssertEqual(0x00000000FFFFFFFFULL,
307 GPBEncodeZigZag64(0xFFFFFFFF80000000LL));
308 XCTAssertEqual(0xFFFFFFFFFFFFFFFEULL,
309 GPBEncodeZigZag64(0x7FFFFFFFFFFFFFFFLL));
310 XCTAssertEqual(0xFFFFFFFFFFFFFFFFULL,
311 GPBEncodeZigZag64(0x8000000000000000LL));
312
313 // Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1)
314 // were chosen semi-randomly via keyboard bashing.
315 XCTAssertEqual(0U, GPBEncodeZigZag32(GPBDecodeZigZag32(0)));
316 XCTAssertEqual(1U, GPBEncodeZigZag32(GPBDecodeZigZag32(1)));
317 XCTAssertEqual(-1U, GPBEncodeZigZag32(GPBDecodeZigZag32(-1)));
318 XCTAssertEqual(14927U, GPBEncodeZigZag32(GPBDecodeZigZag32(14927)));
319 XCTAssertEqual(-3612U, GPBEncodeZigZag32(GPBDecodeZigZag32(-3612)));
320
321 XCTAssertEqual(0ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(0)));
322 XCTAssertEqual(1ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(1)));
323 XCTAssertEqual(-1ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(-1)));
324 XCTAssertEqual(14927ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(14927)));
325 XCTAssertEqual(-3612ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(-3612)));
326
327 XCTAssertEqual(856912304801416ULL,
328 GPBEncodeZigZag64(GPBDecodeZigZag64(856912304801416LL)));
329 XCTAssertEqual(-75123905439571256ULL,
330 GPBEncodeZigZag64(GPBDecodeZigZag64(-75123905439571256LL)));
331}
332
333- (void)testWriteWholeMessage {
334 // Not kGPBDefaultRepeatCount because we are comparing to a golden master file
335 // that was generated with 2.
336 TestAllTypes* message = [self allSetRepeatedCount:2];
337
338 NSData* rawBytes = message.data;
339 NSData* goldenData =
340 [self getDataFileNamed:@"golden_message" dataToWrite:rawBytes];
341 XCTAssertEqualObjects(rawBytes, goldenData);
342
343 // Try different block sizes.
344 for (int blockSize = 1; blockSize < 256; blockSize *= 2) {
345 NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
346 GPBCodedOutputStream* output =
347 [GPBCodedOutputStream streamWithOutputStream:rawOutput
348 bufferSize:blockSize];
349 [message writeToCodedOutputStream:output];
350 [output flush];
351
352 NSData* actual =
353 [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
354 XCTAssertEqualObjects(rawBytes, actual);
355 }
356
357 // Not kGPBDefaultRepeatCount because we are comparing to a golden master file
358 // that was generated with 2.
359 TestAllExtensions* extensions = [self allExtensionsSetRepeatedCount:2];
360 rawBytes = extensions.data;
361 goldenData = [self getDataFileNamed:@"golden_packed_fields_message"
362 dataToWrite:rawBytes];
363 XCTAssertEqualObjects(rawBytes, goldenData);
364}
365
Austin Schuh40c16522018-10-28 20:27:54 -0700366- (void)testCFStringGetCStringPtrAndStringsWithNullChars {
367 // This test exists to verify that CFStrings with embedded NULLs still expose
368 // their raw buffer if they are backed by UTF8 storage. If this fails, the
369 // quick/direct access paths in GPBCodedOutputStream that depend on
370 // CFStringGetCStringPtr need to be re-evalutated (maybe just removed).
371 // And yes, we do get NULLs in strings from some servers.
372
373 char zeroTest[] = "\0Test\0String";
374 // Note: there is a \0 at the end of this since it is a c-string.
375 NSString *asNSString = [[NSString alloc] initWithBytes:zeroTest
376 length:sizeof(zeroTest)
377 encoding:NSUTF8StringEncoding];
378 const char *cString =
379 CFStringGetCStringPtr((CFStringRef)asNSString, kCFStringEncodingUTF8);
380 XCTAssertTrue(cString != NULL);
381 // Again, if the above assert fails, then it means NSString no longer exposes
382 // the raw utf8 storage of a string created from utf8 input, so the code using
383 // CFStringGetCStringPtr in GPBCodedOutputStream will still work (it will take
384 // a different code path); but the optimizations for when
385 // CFStringGetCStringPtr does work could possibly go away.
386
387 XCTAssertEqual(sizeof(zeroTest),
388 [asNSString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
389 XCTAssertTrue(0 == memcmp(cString, zeroTest, sizeof(zeroTest)));
390 [asNSString release];
391}
392
393- (void)testWriteStringsWithZeroChar {
394 // Unicode allows `\0` as a character, and NSString is a class cluster, so
395 // there are a few different classes that could end up beind a given string.
396 // Historically, we've seen differences based on constant strings in code and
397 // strings built via the NSString apis. So this round trips them to ensure
398 // they are acting as expected.
399
400 NSArray<NSString *> *strs = @[
401 @"\0at start",
402 @"in\0middle",
403 @"at end\0",
404 ];
405 int i = 0;
406 for (NSString *str in strs) {
407 NSData *asUTF8 = [str dataUsingEncoding:NSUTF8StringEncoding];
408 NSMutableData *expected = [NSMutableData data];
409 uint8_t lengthByte = (uint8_t)asUTF8.length;
410 [expected appendBytes:&lengthByte length:1];
411 [expected appendData:asUTF8];
412
413 NSString *context = [NSString stringWithFormat:@"Loop %d - Literal", i];
414 [self assertWriteStringNoTag:expected value:str context:context];
415
416 // Force a new string to be built which gets a different class from the
417 // NSString class cluster than the literal did.
418 NSString *str2 = [NSString stringWithFormat:@"%@", str];
419 context = [NSString stringWithFormat:@"Loop %d - Built", i];
420 [self assertWriteStringNoTag:expected value:str2 context:context];
421
422 ++i;
423 }
424}
425
426- (void)testThatItThrowsWhenWriteRawPtrFails {
427 NSOutputStream *output = [NSOutputStream outputStreamToMemory];
428 GPBCodedOutputStream *codedOutput =
429 [GPBCodedOutputStream streamWithOutputStream:output bufferSize:0]; // Skip buffering.
430 [output close]; // Close the output stream to force failure on write.
431 const char *cString = "raw";
432 XCTAssertThrowsSpecificNamed([codedOutput writeRawPtr:cString offset:0 length:strlen(cString)],
433 NSException, GPBCodedOutputStreamException_WriteFailed);
434}
435
Brian Silverman9c614bc2016-02-15 20:20:02 -0500436@end