GPBCodedInputStreamTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #import "GPBTestUtilities.h"
  31. #import "GPBCodedInputStream.h"
  32. #import "GPBCodedOutputStream.h"
  33. #import "GPBUnknownFieldSet_PackagePrivate.h"
  34. #import "GPBUtilities_PackagePrivate.h"
  35. #import "google/protobuf/Unittest.pbobjc.h"
  36. @interface CodedInputStreamTests : GPBTestCase
  37. @end
  38. @implementation CodedInputStreamTests
  39. - (NSData*)bytes_with_sentinel:(int32_t)unused, ... {
  40. va_list list;
  41. va_start(list, unused);
  42. NSMutableData* values = [NSMutableData dataWithCapacity:0];
  43. int32_t i;
  44. while ((i = va_arg(list, int32_t)) != 256) {
  45. NSAssert(i >= 0 && i < 256, @"");
  46. uint8_t u = (uint8_t)i;
  47. [values appendBytes:&u length:1];
  48. }
  49. va_end(list);
  50. return values;
  51. }
  52. #define bytes(...) [self bytes_with_sentinel:0, __VA_ARGS__, 256]
  53. - (void)testDecodeZigZag {
  54. XCTAssertEqual(0, GPBDecodeZigZag32(0));
  55. XCTAssertEqual(-1, GPBDecodeZigZag32(1));
  56. XCTAssertEqual(1, GPBDecodeZigZag32(2));
  57. XCTAssertEqual(-2, GPBDecodeZigZag32(3));
  58. XCTAssertEqual((int32_t)0x3FFFFFFF, GPBDecodeZigZag32(0x7FFFFFFE));
  59. XCTAssertEqual((int32_t)0xC0000000, GPBDecodeZigZag32(0x7FFFFFFF));
  60. XCTAssertEqual((int32_t)0x7FFFFFFF, GPBDecodeZigZag32(0xFFFFFFFE));
  61. XCTAssertEqual((int32_t)0x80000000, GPBDecodeZigZag32(0xFFFFFFFF));
  62. XCTAssertEqual((int64_t)0, GPBDecodeZigZag64(0));
  63. XCTAssertEqual((int64_t)-1, GPBDecodeZigZag64(1));
  64. XCTAssertEqual((int64_t)1, GPBDecodeZigZag64(2));
  65. XCTAssertEqual((int64_t)-2, GPBDecodeZigZag64(3));
  66. XCTAssertEqual((int64_t)0x000000003FFFFFFFL,
  67. GPBDecodeZigZag64(0x000000007FFFFFFEL));
  68. XCTAssertEqual((int64_t)0xFFFFFFFFC0000000L,
  69. GPBDecodeZigZag64(0x000000007FFFFFFFL));
  70. XCTAssertEqual((int64_t)0x000000007FFFFFFFL,
  71. GPBDecodeZigZag64(0x00000000FFFFFFFEL));
  72. XCTAssertEqual((int64_t)0xFFFFFFFF80000000L,
  73. GPBDecodeZigZag64(0x00000000FFFFFFFFL));
  74. XCTAssertEqual((int64_t)0x7FFFFFFFFFFFFFFFL,
  75. GPBDecodeZigZag64(0xFFFFFFFFFFFFFFFEL));
  76. XCTAssertEqual((int64_t)0x8000000000000000L,
  77. GPBDecodeZigZag64(0xFFFFFFFFFFFFFFFFL));
  78. }
  79. - (void)assertReadVarint:(NSData*)data value:(int64_t)value {
  80. {
  81. GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data];
  82. XCTAssertEqual((int32_t)value, [input readInt32]);
  83. }
  84. {
  85. GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data];
  86. XCTAssertEqual(value, [input readInt64]);
  87. }
  88. }
  89. - (void)assertReadLittleEndian32:(NSData*)data value:(int32_t)value {
  90. GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data];
  91. XCTAssertEqual(value, [input readSFixed32]);
  92. }
  93. - (void)assertReadLittleEndian64:(NSData*)data value:(int64_t)value {
  94. GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data];
  95. XCTAssertEqual(value, [input readSFixed64]);
  96. }
  97. - (void)assertReadVarintFailure:(NSData*)data {
  98. {
  99. GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data];
  100. XCTAssertThrows([input readInt32]);
  101. }
  102. {
  103. GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data];
  104. XCTAssertThrows([input readInt64]);
  105. }
  106. }
  107. - (void)testBytes {
  108. NSData* data = bytes(0xa2, 0x74);
  109. XCTAssertEqual(data.length, (NSUInteger)2);
  110. XCTAssertEqual(((uint8_t*)data.bytes)[0], (uint8_t)0xa2);
  111. XCTAssertEqual(((uint8_t*)data.bytes)[1], (uint8_t)0x74);
  112. }
  113. - (void)testReadVarint {
  114. [self assertReadVarint:bytes(0x00) value:0];
  115. [self assertReadVarint:bytes(0x01) value:1];
  116. [self assertReadVarint:bytes(0x7f) value:127];
  117. // 14882
  118. [self assertReadVarint:bytes(0xa2, 0x74) value:(0x22 << 0) | (0x74 << 7)];
  119. // 2961488830
  120. [self assertReadVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b)
  121. value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) |
  122. (0x04 << 21) | (0x0bLL << 28)];
  123. // 64-bit
  124. // 7256456126
  125. [self assertReadVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b)
  126. value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) |
  127. (0x04 << 21) | (0x1bLL << 28)];
  128. // 41256202580718336
  129. [self assertReadVarint:bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49)
  130. value:(0x00 << 0) | (0x66 << 7) | (0x6b << 14) |
  131. (0x1c << 21) | (0x43LL << 28) | (0x49LL << 35) |
  132. (0x24LL << 42) | (0x49LL << 49)];
  133. // 11964378330978735131
  134. [self
  135. assertReadVarint:bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85,
  136. 0xa6, 0x01)
  137. value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
  138. (0x3bLL << 28) | (0x56LL << 35) | (0x00LL << 42) |
  139. (0x05LL << 49) | (0x26LL << 56) | (0x01LL << 63)];
  140. // Failures
  141. [self assertReadVarintFailure:bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  142. 0x80, 0x80, 0x80, 0x00)];
  143. [self assertReadVarintFailure:bytes(0x80)];
  144. }
  145. - (void)testReadLittleEndian {
  146. [self assertReadLittleEndian32:bytes(0x78, 0x56, 0x34, 0x12)
  147. value:0x12345678];
  148. [self assertReadLittleEndian32:bytes(0xf0, 0xde, 0xbc, 0x9a)
  149. value:0x9abcdef0];
  150. [self assertReadLittleEndian64:bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34,
  151. 0x12)
  152. value:0x123456789abcdef0LL];
  153. [self assertReadLittleEndian64:bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc,
  154. 0x9a)
  155. value:0x9abcdef012345678LL];
  156. }
  157. - (void)testReadWholeMessage {
  158. TestAllTypes* message = [self allSetRepeatedCount:kGPBDefaultRepeatCount];
  159. NSData* rawBytes = message.data;
  160. XCTAssertEqual(message.serializedSize, (size_t)rawBytes.length);
  161. TestAllTypes* message2 =
  162. [TestAllTypes parseFromData:rawBytes extensionRegistry:nil];
  163. [self assertAllFieldsSet:message2 repeatedCount:kGPBDefaultRepeatCount];
  164. }
  165. - (void)testSkipWholeMessage {
  166. TestAllTypes* message = [self allSetRepeatedCount:kGPBDefaultRepeatCount];
  167. NSData* rawBytes = message.data;
  168. // Create two parallel inputs. Parse one as unknown fields while using
  169. // skipField() to skip each field on the other. Expect the same tags.
  170. GPBCodedInputStream* input1 = [GPBCodedInputStream streamWithData:rawBytes];
  171. GPBCodedInputStream* input2 = [GPBCodedInputStream streamWithData:rawBytes];
  172. GPBUnknownFieldSet* unknownFields =
  173. [[[GPBUnknownFieldSet alloc] init] autorelease];
  174. while (YES) {
  175. int32_t tag = [input1 readTag];
  176. XCTAssertEqual(tag, [input2 readTag]);
  177. if (tag == 0) {
  178. break;
  179. }
  180. [unknownFields mergeFieldFrom:tag input:input1];
  181. [input2 skipField:tag];
  182. }
  183. }
  184. - (void)testReadHugeBlob {
  185. // Allocate and initialize a 1MB blob.
  186. NSMutableData* blob = [NSMutableData dataWithLength:1 << 20];
  187. for (NSUInteger i = 0; i < blob.length; i++) {
  188. ((uint8_t*)blob.mutableBytes)[i] = (uint8_t)i;
  189. }
  190. // Make a message containing it.
  191. TestAllTypes* message = [TestAllTypes message];
  192. [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount];
  193. [message setOptionalBytes:blob];
  194. // Serialize and parse it. Make sure to parse from an InputStream, not
  195. // directly from a ByteString, so that CodedInputStream uses buffered
  196. // reading.
  197. GPBCodedInputStream* stream =
  198. [GPBCodedInputStream streamWithData:message.data];
  199. TestAllTypes* message2 =
  200. [TestAllTypes parseFromCodedInputStream:stream extensionRegistry:nil];
  201. XCTAssertEqualObjects(message.optionalBytes, message2.optionalBytes);
  202. // Make sure all the other fields were parsed correctly.
  203. TestAllTypes* message3 = [[message2 copy] autorelease];
  204. TestAllTypes* types = [self allSetRepeatedCount:kGPBDefaultRepeatCount];
  205. NSData* data = [types optionalBytes];
  206. [message3 setOptionalBytes:data];
  207. [self assertAllFieldsSet:message3 repeatedCount:kGPBDefaultRepeatCount];
  208. }
  209. - (void)testReadMaliciouslyLargeBlob {
  210. NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
  211. GPBCodedOutputStream* output =
  212. [GPBCodedOutputStream streamWithOutputStream:rawOutput];
  213. int32_t tag = GPBWireFormatMakeTag(1, GPBWireFormatLengthDelimited);
  214. [output writeRawVarint32:tag];
  215. [output writeRawVarint32:0x7FFFFFFF];
  216. uint8_t bytes[32] = {0};
  217. [output writeRawData:[NSData dataWithBytes:bytes length:32]];
  218. [output flush];
  219. NSData* data =
  220. [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  221. GPBCodedInputStream* input =
  222. [GPBCodedInputStream streamWithData:[NSMutableData dataWithData:data]];
  223. XCTAssertEqual(tag, [input readTag]);
  224. XCTAssertThrows([input readData]);
  225. }
  226. // Verifies fix for b/10315336.
  227. - (void)testReadMalformedString {
  228. NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
  229. GPBCodedOutputStream* output =
  230. [GPBCodedOutputStream streamWithOutputStream:rawOutput];
  231. int32_t tag = GPBWireFormatMakeTag(TestAllTypes_FieldNumber_DefaultString,
  232. GPBWireFormatLengthDelimited);
  233. [output writeRawVarint32:tag];
  234. [output writeRawVarint32:5];
  235. // Create an invalid utf-8 byte array.
  236. uint8_t bytes[5] = {0xc2, 0xf2};
  237. [output writeRawData:[NSData dataWithBytes:bytes length:sizeof(bytes)]];
  238. [output flush];
  239. NSData* data =
  240. [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  241. GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data];
  242. TestAllTypes* message =
  243. [TestAllTypes parseFromCodedInputStream:input extensionRegistry:nil];
  244. // Make sure we can read string properties twice without crashing.
  245. XCTAssertEqual([message.defaultString length], (NSUInteger)0);
  246. XCTAssertEqualObjects(@"", message.defaultString);
  247. }
  248. @end