/thirdparty/breakpad/third_party/protobuf/protobuf/java/src/test/java/com/google/protobuf/CodedInputStreamTest.java

http://github.com/tomahawk-player/tomahawk · Java · 528 lines · 347 code · 66 blank · 115 comment · 17 complexity · b097f41aa7ada591a86833c3786334c7 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. package com.google.protobuf;
  31. import protobuf_unittest.UnittestProto.TestAllTypes;
  32. import protobuf_unittest.UnittestProto.TestRecursiveMessage;
  33. import junit.framework.TestCase;
  34. import java.io.ByteArrayInputStream;
  35. import java.io.FilterInputStream;
  36. import java.io.InputStream;
  37. import java.io.IOException;
  38. /**
  39. * Unit test for {@link CodedInputStream}.
  40. *
  41. * @author kenton@google.com Kenton Varda
  42. */
  43. public class CodedInputStreamTest extends TestCase {
  44. /**
  45. * Helper to construct a byte array from a bunch of bytes. The inputs are
  46. * actually ints so that I can use hex notation and not get stupid errors
  47. * about precision.
  48. */
  49. private byte[] bytes(int... bytesAsInts) {
  50. byte[] bytes = new byte[bytesAsInts.length];
  51. for (int i = 0; i < bytesAsInts.length; i++) {
  52. bytes[i] = (byte) bytesAsInts[i];
  53. }
  54. return bytes;
  55. }
  56. /**
  57. * An InputStream which limits the number of bytes it reads at a time.
  58. * We use this to make sure that CodedInputStream doesn't screw up when
  59. * reading in small blocks.
  60. */
  61. private static final class SmallBlockInputStream extends FilterInputStream {
  62. private final int blockSize;
  63. public SmallBlockInputStream(byte[] data, int blockSize) {
  64. this(new ByteArrayInputStream(data), blockSize);
  65. }
  66. public SmallBlockInputStream(InputStream in, int blockSize) {
  67. super(in);
  68. this.blockSize = blockSize;
  69. }
  70. public int read(byte[] b) throws IOException {
  71. return super.read(b, 0, Math.min(b.length, blockSize));
  72. }
  73. public int read(byte[] b, int off, int len) throws IOException {
  74. return super.read(b, off, Math.min(len, blockSize));
  75. }
  76. }
  77. /**
  78. * Parses the given bytes using readRawVarint32() and readRawVarint64() and
  79. * checks that the result matches the given value.
  80. */
  81. private void assertReadVarint(byte[] data, long value) throws Exception {
  82. CodedInputStream input = CodedInputStream.newInstance(data);
  83. assertEquals((int)value, input.readRawVarint32());
  84. input = CodedInputStream.newInstance(data);
  85. assertEquals(value, input.readRawVarint64());
  86. assertTrue(input.isAtEnd());
  87. // Try different block sizes.
  88. for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
  89. input = CodedInputStream.newInstance(
  90. new SmallBlockInputStream(data, blockSize));
  91. assertEquals((int)value, input.readRawVarint32());
  92. input = CodedInputStream.newInstance(
  93. new SmallBlockInputStream(data, blockSize));
  94. assertEquals(value, input.readRawVarint64());
  95. assertTrue(input.isAtEnd());
  96. }
  97. // Try reading direct from an InputStream. We want to verify that it
  98. // doesn't read past the end of the input, so we copy to a new, bigger
  99. // array first.
  100. byte[] longerData = new byte[data.length + 1];
  101. System.arraycopy(data, 0, longerData, 0, data.length);
  102. InputStream rawInput = new ByteArrayInputStream(longerData);
  103. assertEquals((int)value, CodedInputStream.readRawVarint32(rawInput));
  104. assertEquals(1, rawInput.available());
  105. }
  106. /**
  107. * Parses the given bytes using readRawVarint32() and readRawVarint64() and
  108. * expects them to fail with an InvalidProtocolBufferException whose
  109. * description matches the given one.
  110. */
  111. private void assertReadVarintFailure(
  112. InvalidProtocolBufferException expected, byte[] data)
  113. throws Exception {
  114. CodedInputStream input = CodedInputStream.newInstance(data);
  115. try {
  116. input.readRawVarint32();
  117. fail("Should have thrown an exception.");
  118. } catch (InvalidProtocolBufferException e) {
  119. assertEquals(expected.getMessage(), e.getMessage());
  120. }
  121. input = CodedInputStream.newInstance(data);
  122. try {
  123. input.readRawVarint64();
  124. fail("Should have thrown an exception.");
  125. } catch (InvalidProtocolBufferException e) {
  126. assertEquals(expected.getMessage(), e.getMessage());
  127. }
  128. // Make sure we get the same error when reading direct from an InputStream.
  129. try {
  130. CodedInputStream.readRawVarint32(new ByteArrayInputStream(data));
  131. fail("Should have thrown an exception.");
  132. } catch (InvalidProtocolBufferException e) {
  133. assertEquals(expected.getMessage(), e.getMessage());
  134. }
  135. }
  136. /** Tests readRawVarint32() and readRawVarint64(). */
  137. public void testReadVarint() throws Exception {
  138. assertReadVarint(bytes(0x00), 0);
  139. assertReadVarint(bytes(0x01), 1);
  140. assertReadVarint(bytes(0x7f), 127);
  141. // 14882
  142. assertReadVarint(bytes(0xa2, 0x74), (0x22 << 0) | (0x74 << 7));
  143. // 2961488830
  144. assertReadVarint(bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b),
  145. (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
  146. (0x0bL << 28));
  147. // 64-bit
  148. // 7256456126
  149. assertReadVarint(bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b),
  150. (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
  151. (0x1bL << 28));
  152. // 41256202580718336
  153. assertReadVarint(
  154. bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49),
  155. (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
  156. (0x43L << 28) | (0x49L << 35) | (0x24L << 42) | (0x49L << 49));
  157. // 11964378330978735131
  158. assertReadVarint(
  159. bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01),
  160. (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
  161. (0x3bL << 28) | (0x56L << 35) | (0x00L << 42) |
  162. (0x05L << 49) | (0x26L << 56) | (0x01L << 63));
  163. // Failures
  164. assertReadVarintFailure(
  165. InvalidProtocolBufferException.malformedVarint(),
  166. bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  167. 0x00));
  168. assertReadVarintFailure(
  169. InvalidProtocolBufferException.truncatedMessage(),
  170. bytes(0x80));
  171. }
  172. /**
  173. * Parses the given bytes using readRawLittleEndian32() and checks
  174. * that the result matches the given value.
  175. */
  176. private void assertReadLittleEndian32(byte[] data, int value)
  177. throws Exception {
  178. CodedInputStream input = CodedInputStream.newInstance(data);
  179. assertEquals(value, input.readRawLittleEndian32());
  180. assertTrue(input.isAtEnd());
  181. // Try different block sizes.
  182. for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
  183. input = CodedInputStream.newInstance(
  184. new SmallBlockInputStream(data, blockSize));
  185. assertEquals(value, input.readRawLittleEndian32());
  186. assertTrue(input.isAtEnd());
  187. }
  188. }
  189. /**
  190. * Parses the given bytes using readRawLittleEndian64() and checks
  191. * that the result matches the given value.
  192. */
  193. private void assertReadLittleEndian64(byte[] data, long value)
  194. throws Exception {
  195. CodedInputStream input = CodedInputStream.newInstance(data);
  196. assertEquals(value, input.readRawLittleEndian64());
  197. assertTrue(input.isAtEnd());
  198. // Try different block sizes.
  199. for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
  200. input = CodedInputStream.newInstance(
  201. new SmallBlockInputStream(data, blockSize));
  202. assertEquals(value, input.readRawLittleEndian64());
  203. assertTrue(input.isAtEnd());
  204. }
  205. }
  206. /** Tests readRawLittleEndian32() and readRawLittleEndian64(). */
  207. public void testReadLittleEndian() throws Exception {
  208. assertReadLittleEndian32(bytes(0x78, 0x56, 0x34, 0x12), 0x12345678);
  209. assertReadLittleEndian32(bytes(0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef0);
  210. assertReadLittleEndian64(
  211. bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12),
  212. 0x123456789abcdef0L);
  213. assertReadLittleEndian64(
  214. bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a),
  215. 0x9abcdef012345678L);
  216. }
  217. /** Test decodeZigZag32() and decodeZigZag64(). */
  218. public void testDecodeZigZag() throws Exception {
  219. assertEquals( 0, CodedInputStream.decodeZigZag32(0));
  220. assertEquals(-1, CodedInputStream.decodeZigZag32(1));
  221. assertEquals( 1, CodedInputStream.decodeZigZag32(2));
  222. assertEquals(-2, CodedInputStream.decodeZigZag32(3));
  223. assertEquals(0x3FFFFFFF, CodedInputStream.decodeZigZag32(0x7FFFFFFE));
  224. assertEquals(0xC0000000, CodedInputStream.decodeZigZag32(0x7FFFFFFF));
  225. assertEquals(0x7FFFFFFF, CodedInputStream.decodeZigZag32(0xFFFFFFFE));
  226. assertEquals(0x80000000, CodedInputStream.decodeZigZag32(0xFFFFFFFF));
  227. assertEquals( 0, CodedInputStream.decodeZigZag64(0));
  228. assertEquals(-1, CodedInputStream.decodeZigZag64(1));
  229. assertEquals( 1, CodedInputStream.decodeZigZag64(2));
  230. assertEquals(-2, CodedInputStream.decodeZigZag64(3));
  231. assertEquals(0x000000003FFFFFFFL,
  232. CodedInputStream.decodeZigZag64(0x000000007FFFFFFEL));
  233. assertEquals(0xFFFFFFFFC0000000L,
  234. CodedInputStream.decodeZigZag64(0x000000007FFFFFFFL));
  235. assertEquals(0x000000007FFFFFFFL,
  236. CodedInputStream.decodeZigZag64(0x00000000FFFFFFFEL));
  237. assertEquals(0xFFFFFFFF80000000L,
  238. CodedInputStream.decodeZigZag64(0x00000000FFFFFFFFL));
  239. assertEquals(0x7FFFFFFFFFFFFFFFL,
  240. CodedInputStream.decodeZigZag64(0xFFFFFFFFFFFFFFFEL));
  241. assertEquals(0x8000000000000000L,
  242. CodedInputStream.decodeZigZag64(0xFFFFFFFFFFFFFFFFL));
  243. }
  244. /** Tests reading and parsing a whole message with every field type. */
  245. public void testReadWholeMessage() throws Exception {
  246. TestAllTypes message = TestUtil.getAllSet();
  247. byte[] rawBytes = message.toByteArray();
  248. assertEquals(rawBytes.length, message.getSerializedSize());
  249. TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes);
  250. TestUtil.assertAllFieldsSet(message2);
  251. // Try different block sizes.
  252. for (int blockSize = 1; blockSize < 256; blockSize *= 2) {
  253. message2 = TestAllTypes.parseFrom(
  254. new SmallBlockInputStream(rawBytes, blockSize));
  255. TestUtil.assertAllFieldsSet(message2);
  256. }
  257. }
  258. /** Tests skipField(). */
  259. public void testSkipWholeMessage() throws Exception {
  260. TestAllTypes message = TestUtil.getAllSet();
  261. byte[] rawBytes = message.toByteArray();
  262. // Create two parallel inputs. Parse one as unknown fields while using
  263. // skipField() to skip each field on the other. Expect the same tags.
  264. CodedInputStream input1 = CodedInputStream.newInstance(rawBytes);
  265. CodedInputStream input2 = CodedInputStream.newInstance(rawBytes);
  266. UnknownFieldSet.Builder unknownFields = UnknownFieldSet.newBuilder();
  267. while (true) {
  268. int tag = input1.readTag();
  269. assertEquals(tag, input2.readTag());
  270. if (tag == 0) {
  271. break;
  272. }
  273. unknownFields.mergeFieldFrom(tag, input1);
  274. input2.skipField(tag);
  275. }
  276. }
  277. /**
  278. * Test that a bug in skipRawBytes() has been fixed: if the skip skips
  279. * exactly up to a limit, this should not break things.
  280. */
  281. public void testSkipRawBytesBug() throws Exception {
  282. byte[] rawBytes = new byte[] { 1, 2 };
  283. CodedInputStream input = CodedInputStream.newInstance(rawBytes);
  284. int limit = input.pushLimit(1);
  285. input.skipRawBytes(1);
  286. input.popLimit(limit);
  287. assertEquals(2, input.readRawByte());
  288. }
  289. /**
  290. * Test that a bug in skipRawBytes() has been fixed: if the skip skips
  291. * past the end of a buffer with a limit that has been set past the end of
  292. * that buffer, this should not break things.
  293. */
  294. public void testSkipRawBytesPastEndOfBufferWithLimit() throws Exception {
  295. byte[] rawBytes = new byte[] { 1, 2, 3, 4, 5 };
  296. CodedInputStream input = CodedInputStream.newInstance(
  297. new SmallBlockInputStream(rawBytes, 3));
  298. int limit = input.pushLimit(4);
  299. // In order to expose the bug we need to read at least one byte to prime the
  300. // buffer inside the CodedInputStream.
  301. assertEquals(1, input.readRawByte());
  302. // Skip to the end of the limit.
  303. input.skipRawBytes(3);
  304. assertTrue(input.isAtEnd());
  305. input.popLimit(limit);
  306. assertEquals(5, input.readRawByte());
  307. }
  308. public void testReadHugeBlob() throws Exception {
  309. // Allocate and initialize a 1MB blob.
  310. byte[] blob = new byte[1 << 20];
  311. for (int i = 0; i < blob.length; i++) {
  312. blob[i] = (byte)i;
  313. }
  314. // Make a message containing it.
  315. TestAllTypes.Builder builder = TestAllTypes.newBuilder();
  316. TestUtil.setAllFields(builder);
  317. builder.setOptionalBytes(ByteString.copyFrom(blob));
  318. TestAllTypes message = builder.build();
  319. // Serialize and parse it. Make sure to parse from an InputStream, not
  320. // directly from a ByteString, so that CodedInputStream uses buffered
  321. // reading.
  322. TestAllTypes message2 =
  323. TestAllTypes.parseFrom(message.toByteString().newInput());
  324. assertEquals(message.getOptionalBytes(), message2.getOptionalBytes());
  325. // Make sure all the other fields were parsed correctly.
  326. TestAllTypes message3 = TestAllTypes.newBuilder(message2)
  327. .setOptionalBytes(TestUtil.getAllSet().getOptionalBytes())
  328. .build();
  329. TestUtil.assertAllFieldsSet(message3);
  330. }
  331. public void testReadMaliciouslyLargeBlob() throws Exception {
  332. ByteString.Output rawOutput = ByteString.newOutput();
  333. CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
  334. int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
  335. output.writeRawVarint32(tag);
  336. output.writeRawVarint32(0x7FFFFFFF);
  337. output.writeRawBytes(new byte[32]); // Pad with a few random bytes.
  338. output.flush();
  339. CodedInputStream input = rawOutput.toByteString().newCodedInput();
  340. assertEquals(tag, input.readTag());
  341. try {
  342. input.readBytes();
  343. fail("Should have thrown an exception!");
  344. } catch (InvalidProtocolBufferException e) {
  345. // success.
  346. }
  347. }
  348. private TestRecursiveMessage makeRecursiveMessage(int depth) {
  349. if (depth == 0) {
  350. return TestRecursiveMessage.newBuilder().setI(5).build();
  351. } else {
  352. return TestRecursiveMessage.newBuilder()
  353. .setA(makeRecursiveMessage(depth - 1)).build();
  354. }
  355. }
  356. private void assertMessageDepth(TestRecursiveMessage message, int depth) {
  357. if (depth == 0) {
  358. assertFalse(message.hasA());
  359. assertEquals(5, message.getI());
  360. } else {
  361. assertTrue(message.hasA());
  362. assertMessageDepth(message.getA(), depth - 1);
  363. }
  364. }
  365. public void testMaliciousRecursion() throws Exception {
  366. ByteString data64 = makeRecursiveMessage(64).toByteString();
  367. ByteString data65 = makeRecursiveMessage(65).toByteString();
  368. assertMessageDepth(TestRecursiveMessage.parseFrom(data64), 64);
  369. try {
  370. TestRecursiveMessage.parseFrom(data65);
  371. fail("Should have thrown an exception!");
  372. } catch (InvalidProtocolBufferException e) {
  373. // success.
  374. }
  375. CodedInputStream input = data64.newCodedInput();
  376. input.setRecursionLimit(8);
  377. try {
  378. TestRecursiveMessage.parseFrom(input);
  379. fail("Should have thrown an exception!");
  380. } catch (InvalidProtocolBufferException e) {
  381. // success.
  382. }
  383. }
  384. public void testSizeLimit() throws Exception {
  385. CodedInputStream input = CodedInputStream.newInstance(
  386. TestUtil.getAllSet().toByteString().newInput());
  387. input.setSizeLimit(16);
  388. try {
  389. TestAllTypes.parseFrom(input);
  390. fail("Should have thrown an exception!");
  391. } catch (InvalidProtocolBufferException e) {
  392. // success.
  393. }
  394. }
  395. public void testResetSizeCounter() throws Exception {
  396. CodedInputStream input = CodedInputStream.newInstance(
  397. new SmallBlockInputStream(new byte[256], 8));
  398. input.setSizeLimit(16);
  399. input.readRawBytes(16);
  400. assertEquals(16, input.getTotalBytesRead());
  401. try {
  402. input.readRawByte();
  403. fail("Should have thrown an exception!");
  404. } catch (InvalidProtocolBufferException e) {
  405. // success.
  406. }
  407. input.resetSizeCounter();
  408. assertEquals(0, input.getTotalBytesRead());
  409. input.readRawByte(); // No exception thrown.
  410. input.resetSizeCounter();
  411. assertEquals(0, input.getTotalBytesRead());
  412. try {
  413. input.readRawBytes(16); // Hits limit again.
  414. fail("Should have thrown an exception!");
  415. } catch (InvalidProtocolBufferException e) {
  416. // success.
  417. }
  418. }
  419. /**
  420. * Tests that if we read an string that contains invalid UTF-8, no exception
  421. * is thrown. Instead, the invalid bytes are replaced with the Unicode
  422. * "replacement character" U+FFFD.
  423. */
  424. public void testReadInvalidUtf8() throws Exception {
  425. ByteString.Output rawOutput = ByteString.newOutput();
  426. CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
  427. int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
  428. output.writeRawVarint32(tag);
  429. output.writeRawVarint32(1);
  430. output.writeRawBytes(new byte[] { (byte)0x80 });
  431. output.flush();
  432. CodedInputStream input = rawOutput.toByteString().newCodedInput();
  433. assertEquals(tag, input.readTag());
  434. String text = input.readString();
  435. assertEquals(0xfffd, text.charAt(0));
  436. }
  437. public void testReadFromSlice() throws Exception {
  438. byte[] bytes = bytes(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  439. CodedInputStream in = CodedInputStream.newInstance(bytes, 3, 5);
  440. assertEquals(0, in.getTotalBytesRead());
  441. for (int i = 3; i < 8; i++) {
  442. assertEquals(i, in.readRawByte());
  443. assertEquals(i-2, in.getTotalBytesRead());
  444. }
  445. // eof
  446. assertEquals(0, in.readTag());
  447. assertEquals(5, in.getTotalBytesRead());
  448. }
  449. public void testInvalidTag() throws Exception {
  450. // Any tag number which corresponds to field number zero is invalid and
  451. // should throw InvalidProtocolBufferException.
  452. for (int i = 0; i < 8; i++) {
  453. try {
  454. CodedInputStream.newInstance(bytes(i)).readTag();
  455. fail("Should have thrown an exception.");
  456. } catch (InvalidProtocolBufferException e) {
  457. assertEquals(InvalidProtocolBufferException.invalidTag().getMessage(),
  458. e.getMessage());
  459. }
  460. }
  461. }
  462. }