CodedInputStreamTest.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.Collections.Generic;
  36. using System.IO;
  37. using Google.ProtocolBuffers.TestProtos;
  38. using NUnit.Framework;
  39. using System.Diagnostics;
  40. namespace Google.ProtocolBuffers
  41. {
  42. [TestFixture]
  43. public class CodedInputStreamTest
  44. {
  45. /// <summary>
  46. /// Helper to construct a byte array from a bunch of bytes. The inputs are
  47. /// actually ints so that I can use hex notation and not get stupid errors
  48. /// about precision.
  49. /// </summary>
  50. private static byte[] Bytes(params int[] bytesAsInts)
  51. {
  52. byte[] bytes = new byte[bytesAsInts.Length];
  53. for (int i = 0; i < bytesAsInts.Length; i++)
  54. {
  55. bytes[i] = (byte) bytesAsInts[i];
  56. }
  57. return bytes;
  58. }
  59. /// <summary>
  60. /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and
  61. /// </summary>
  62. private static void AssertReadVarint(byte[] data, ulong value)
  63. {
  64. CodedInputStream input = CodedInputStream.CreateInstance(data);
  65. Assert.AreEqual((uint) value, input.ReadRawVarint32());
  66. input = CodedInputStream.CreateInstance(data);
  67. Assert.AreEqual(value, input.ReadRawVarint64());
  68. Assert.IsTrue(input.IsAtEnd);
  69. // Try different block sizes.
  70. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
  71. {
  72. input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize));
  73. Assert.AreEqual((uint) value, input.ReadRawVarint32());
  74. input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize));
  75. Assert.AreEqual(value, input.ReadRawVarint64());
  76. Assert.IsTrue(input.IsAtEnd);
  77. }
  78. // Try reading directly from a MemoryStream. We want to verify that it
  79. // doesn't read past the end of the input, so write an extra byte - this
  80. // lets us test the position at the end.
  81. MemoryStream memoryStream = new MemoryStream();
  82. memoryStream.Write(data, 0, data.Length);
  83. memoryStream.WriteByte(0);
  84. memoryStream.Position = 0;
  85. Assert.AreEqual((uint) value, CodedInputStream.ReadRawVarint32(memoryStream));
  86. Assert.AreEqual(data.Length, memoryStream.Position);
  87. }
  88. /// <summary>
  89. /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and
  90. /// expects them to fail with an InvalidProtocolBufferException whose
  91. /// description matches the given one.
  92. /// </summary>
  93. private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data)
  94. {
  95. CodedInputStream input = CodedInputStream.CreateInstance(data);
  96. try
  97. {
  98. input.ReadRawVarint32();
  99. Assert.Fail("Should have thrown an exception.");
  100. }
  101. catch (InvalidProtocolBufferException e)
  102. {
  103. Assert.AreEqual(expected.Message, e.Message);
  104. }
  105. input = CodedInputStream.CreateInstance(data);
  106. try
  107. {
  108. input.ReadRawVarint64();
  109. Assert.Fail("Should have thrown an exception.");
  110. }
  111. catch (InvalidProtocolBufferException e)
  112. {
  113. Assert.AreEqual(expected.Message, e.Message);
  114. }
  115. // Make sure we get the same error when reading directly from a Stream.
  116. try
  117. {
  118. CodedInputStream.ReadRawVarint32(new MemoryStream(data));
  119. Assert.Fail("Should have thrown an exception.");
  120. }
  121. catch (InvalidProtocolBufferException e)
  122. {
  123. Assert.AreEqual(expected.Message, e.Message);
  124. }
  125. }
  126. [Test]
  127. public void ReadVarint()
  128. {
  129. AssertReadVarint(Bytes(0x00), 0);
  130. AssertReadVarint(Bytes(0x01), 1);
  131. AssertReadVarint(Bytes(0x7f), 127);
  132. // 14882
  133. AssertReadVarint(Bytes(0xa2, 0x74), (0x22 << 0) | (0x74 << 7));
  134. // 2961488830
  135. AssertReadVarint(Bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b),
  136. (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
  137. (0x0bL << 28));
  138. // 64-bit
  139. // 7256456126
  140. AssertReadVarint(Bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b),
  141. (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
  142. (0x1bL << 28));
  143. // 41256202580718336
  144. AssertReadVarint(Bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49),
  145. (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
  146. (0x43L << 28) | (0x49L << 35) | (0x24L << 42) | (0x49L << 49));
  147. // 11964378330978735131
  148. AssertReadVarint(Bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01),
  149. (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
  150. (0x3bUL << 28) | (0x56UL << 35) | (0x00UL << 42) |
  151. (0x05UL << 49) | (0x26UL << 56) | (0x01UL << 63));
  152. // Failures
  153. AssertReadVarintFailure(
  154. InvalidProtocolBufferException.MalformedVarint(),
  155. Bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  156. 0x00));
  157. AssertReadVarintFailure(
  158. InvalidProtocolBufferException.TruncatedMessage(),
  159. Bytes(0x80));
  160. }
  161. /// <summary>
  162. /// Parses the given bytes using ReadRawLittleEndian32() and checks
  163. /// that the result matches the given value.
  164. /// </summary>
  165. private static void AssertReadLittleEndian32(byte[] data, uint value)
  166. {
  167. CodedInputStream input = CodedInputStream.CreateInstance(data);
  168. Assert.AreEqual(value, input.ReadRawLittleEndian32());
  169. Assert.IsTrue(input.IsAtEnd);
  170. // Try different block sizes.
  171. for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
  172. {
  173. input = CodedInputStream.CreateInstance(
  174. new SmallBlockInputStream(data, blockSize));
  175. Assert.AreEqual(value, input.ReadRawLittleEndian32());
  176. Assert.IsTrue(input.IsAtEnd);
  177. }
  178. }
  179. /// <summary>
  180. /// Parses the given bytes using ReadRawLittleEndian64() and checks
  181. /// that the result matches the given value.
  182. /// </summary>
  183. private static void AssertReadLittleEndian64(byte[] data, ulong value)
  184. {
  185. CodedInputStream input = CodedInputStream.CreateInstance(data);
  186. Assert.AreEqual(value, input.ReadRawLittleEndian64());
  187. Assert.IsTrue(input.IsAtEnd);
  188. // Try different block sizes.
  189. for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
  190. {
  191. input = CodedInputStream.CreateInstance(
  192. new SmallBlockInputStream(data, blockSize));
  193. Assert.AreEqual(value, input.ReadRawLittleEndian64());
  194. Assert.IsTrue(input.IsAtEnd);
  195. }
  196. }
  197. [Test]
  198. public void ReadLittleEndian()
  199. {
  200. AssertReadLittleEndian32(Bytes(0x78, 0x56, 0x34, 0x12), 0x12345678);
  201. AssertReadLittleEndian32(Bytes(0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef0);
  202. AssertReadLittleEndian64(Bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12),
  203. 0x123456789abcdef0L);
  204. AssertReadLittleEndian64(
  205. Bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef012345678UL);
  206. }
  207. [Test]
  208. public void DecodeZigZag32()
  209. {
  210. Assert.AreEqual(0, CodedInputStream.DecodeZigZag32(0));
  211. Assert.AreEqual(-1, CodedInputStream.DecodeZigZag32(1));
  212. Assert.AreEqual(1, CodedInputStream.DecodeZigZag32(2));
  213. Assert.AreEqual(-2, CodedInputStream.DecodeZigZag32(3));
  214. Assert.AreEqual(0x3FFFFFFF, CodedInputStream.DecodeZigZag32(0x7FFFFFFE));
  215. Assert.AreEqual(unchecked((int) 0xC0000000), CodedInputStream.DecodeZigZag32(0x7FFFFFFF));
  216. Assert.AreEqual(0x7FFFFFFF, CodedInputStream.DecodeZigZag32(0xFFFFFFFE));
  217. Assert.AreEqual(unchecked((int) 0x80000000), CodedInputStream.DecodeZigZag32(0xFFFFFFFF));
  218. }
  219. [Test]
  220. public void DecodeZigZag64()
  221. {
  222. Assert.AreEqual(0, CodedInputStream.DecodeZigZag64(0));
  223. Assert.AreEqual(-1, CodedInputStream.DecodeZigZag64(1));
  224. Assert.AreEqual(1, CodedInputStream.DecodeZigZag64(2));
  225. Assert.AreEqual(-2, CodedInputStream.DecodeZigZag64(3));
  226. Assert.AreEqual(0x000000003FFFFFFFL, CodedInputStream.DecodeZigZag64(0x000000007FFFFFFEL));
  227. Assert.AreEqual(unchecked((long) 0xFFFFFFFFC0000000L), CodedInputStream.DecodeZigZag64(0x000000007FFFFFFFL));
  228. Assert.AreEqual(0x000000007FFFFFFFL, CodedInputStream.DecodeZigZag64(0x00000000FFFFFFFEL));
  229. Assert.AreEqual(unchecked((long) 0xFFFFFFFF80000000L), CodedInputStream.DecodeZigZag64(0x00000000FFFFFFFFL));
  230. Assert.AreEqual(0x7FFFFFFFFFFFFFFFL, CodedInputStream.DecodeZigZag64(0xFFFFFFFFFFFFFFFEL));
  231. Assert.AreEqual(unchecked((long) 0x8000000000000000L), CodedInputStream.DecodeZigZag64(0xFFFFFFFFFFFFFFFFL));
  232. }
  233. [Test]
  234. public void ReadWholeMessage()
  235. {
  236. TestAllTypes message = TestUtil.GetAllSet();
  237. byte[] rawBytes = message.ToByteArray();
  238. Assert.AreEqual(rawBytes.Length, message.SerializedSize);
  239. TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
  240. TestUtil.AssertAllFieldsSet(message2);
  241. // Try different block sizes.
  242. for (int blockSize = 1; blockSize < 256; blockSize *= 2)
  243. {
  244. message2 = TestAllTypes.ParseFrom(new SmallBlockInputStream(rawBytes, blockSize));
  245. TestUtil.AssertAllFieldsSet(message2);
  246. }
  247. }
  248. [Test]
  249. public void SkipWholeMessage()
  250. {
  251. TestAllTypes message = TestUtil.GetAllSet();
  252. byte[] rawBytes = message.ToByteArray();
  253. // Create two parallel inputs. Parse one as unknown fields while using
  254. // skipField() to skip each field on the other. Expect the same tags.
  255. CodedInputStream input1 = CodedInputStream.CreateInstance(rawBytes);
  256. CodedInputStream input2 = CodedInputStream.CreateInstance(rawBytes);
  257. UnknownFieldSet.Builder unknownFields = UnknownFieldSet.CreateBuilder();
  258. uint tag;
  259. string name;
  260. while (input1.ReadTag(out tag, out name))
  261. {
  262. uint tag2;
  263. Assert.IsTrue(input2.ReadTag(out tag2, out name));
  264. Assert.AreEqual(tag, tag2);
  265. unknownFields.MergeFieldFrom(tag, input1);
  266. input2.SkipField();
  267. }
  268. }
  269. /// <summary>
  270. /// Test that a bug in SkipRawBytes has been fixed: if the skip
  271. /// skips exactly up to a limit, this should bnot break things
  272. /// </summary>
  273. [Test]
  274. public void SkipRawBytesBug()
  275. {
  276. byte[] rawBytes = new byte[] {1, 2};
  277. CodedInputStream input = CodedInputStream.CreateInstance(rawBytes);
  278. int limit = input.PushLimit(1);
  279. input.SkipRawBytes(1);
  280. input.PopLimit(limit);
  281. Assert.AreEqual(2, input.ReadRawByte());
  282. }
  283. public void ReadHugeBlob()
  284. {
  285. // Allocate and initialize a 1MB blob.
  286. byte[] blob = new byte[1 << 20];
  287. for (int i = 0; i < blob.Length; i++)
  288. {
  289. blob[i] = (byte) i;
  290. }
  291. // Make a message containing it.
  292. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  293. TestUtil.SetAllFields(builder);
  294. builder.SetOptionalBytes(ByteString.CopyFrom(blob));
  295. TestAllTypes message = builder.Build();
  296. // Serialize and parse it. Make sure to parse from an InputStream, not
  297. // directly from a ByteString, so that CodedInputStream uses buffered
  298. // reading.
  299. TestAllTypes message2 = TestAllTypes.ParseFrom(message.ToByteString().CreateCodedInput());
  300. Assert.AreEqual(message.OptionalBytes, message2.OptionalBytes);
  301. // Make sure all the other fields were parsed correctly.
  302. TestAllTypes message3 = TestAllTypes.CreateBuilder(message2)
  303. .SetOptionalBytes(TestUtil.GetAllSet().OptionalBytes)
  304. .Build();
  305. TestUtil.AssertAllFieldsSet(message3);
  306. }
  307. [Test]
  308. public void ReadMaliciouslyLargeBlob()
  309. {
  310. MemoryStream ms = new MemoryStream();
  311. CodedOutputStream output = CodedOutputStream.CreateInstance(ms);
  312. uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
  313. output.WriteRawVarint32(tag);
  314. output.WriteRawVarint32(0x7FFFFFFF);
  315. output.WriteRawBytes(new byte[32]); // Pad with a few random bytes.
  316. output.Flush();
  317. ms.Position = 0;
  318. CodedInputStream input = CodedInputStream.CreateInstance(ms);
  319. uint testtag;
  320. string ignore;
  321. Assert.IsTrue(input.ReadTag(out testtag, out ignore));
  322. Assert.AreEqual(tag, testtag);
  323. try
  324. {
  325. ByteString bytes = null;
  326. input.ReadBytes(ref bytes);
  327. Assert.Fail("Should have thrown an exception!");
  328. }
  329. catch (InvalidProtocolBufferException)
  330. {
  331. // success.
  332. }
  333. }
  334. private static TestRecursiveMessage MakeRecursiveMessage(int depth)
  335. {
  336. if (depth == 0)
  337. {
  338. return TestRecursiveMessage.CreateBuilder().SetI(5).Build();
  339. }
  340. else
  341. {
  342. return TestRecursiveMessage.CreateBuilder()
  343. .SetA(MakeRecursiveMessage(depth - 1)).Build();
  344. }
  345. }
  346. private static void AssertMessageDepth(TestRecursiveMessage message, int depth)
  347. {
  348. if (depth == 0)
  349. {
  350. Assert.IsFalse(message.HasA);
  351. Assert.AreEqual(5, message.I);
  352. }
  353. else
  354. {
  355. Assert.IsTrue(message.HasA);
  356. AssertMessageDepth(message.A, depth - 1);
  357. }
  358. }
  359. [Test]
  360. public void MaliciousRecursion()
  361. {
  362. ByteString data64 = MakeRecursiveMessage(64).ToByteString();
  363. ByteString data65 = MakeRecursiveMessage(65).ToByteString();
  364. AssertMessageDepth(TestRecursiveMessage.ParseFrom(data64), 64);
  365. try
  366. {
  367. TestRecursiveMessage.ParseFrom(data65);
  368. Assert.Fail("Should have thrown an exception!");
  369. }
  370. catch (InvalidProtocolBufferException)
  371. {
  372. // success.
  373. }
  374. CodedInputStream input = data64.CreateCodedInput();
  375. input.SetRecursionLimit(8);
  376. try
  377. {
  378. TestRecursiveMessage.ParseFrom(input);
  379. Assert.Fail("Should have thrown an exception!");
  380. }
  381. catch (InvalidProtocolBufferException)
  382. {
  383. // success.
  384. }
  385. }
  386. [Test]
  387. public void SizeLimit()
  388. {
  389. // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't
  390. // apply to the latter case.
  391. MemoryStream ms = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray());
  392. CodedInputStream input = CodedInputStream.CreateInstance(ms);
  393. input.SetSizeLimit(16);
  394. try
  395. {
  396. TestAllTypes.ParseFrom(input);
  397. Assert.Fail("Should have thrown an exception!");
  398. }
  399. catch (InvalidProtocolBufferException)
  400. {
  401. // success.
  402. }
  403. }
  404. [Test]
  405. public void ResetSizeCounter()
  406. {
  407. CodedInputStream input = CodedInputStream.CreateInstance(
  408. new SmallBlockInputStream(new byte[256], 8));
  409. input.SetSizeLimit(16);
  410. input.ReadRawBytes(16);
  411. try
  412. {
  413. input.ReadRawByte();
  414. Assert.Fail("Should have thrown an exception!");
  415. }
  416. catch (InvalidProtocolBufferException)
  417. {
  418. // Success.
  419. }
  420. input.ResetSizeCounter();
  421. input.ReadRawByte(); // No exception thrown.
  422. try
  423. {
  424. input.ReadRawBytes(16); // Hits limit again.
  425. Assert.Fail("Should have thrown an exception!");
  426. }
  427. catch (InvalidProtocolBufferException)
  428. {
  429. // Success.
  430. }
  431. }
  432. /// <summary>
  433. /// Tests that if we read an string that contains invalid UTF-8, no exception
  434. /// is thrown. Instead, the invalid bytes are replaced with the Unicode
  435. /// "replacement character" U+FFFD.
  436. /// </summary>
  437. [Test]
  438. public void ReadInvalidUtf8()
  439. {
  440. MemoryStream ms = new MemoryStream();
  441. CodedOutputStream output = CodedOutputStream.CreateInstance(ms);
  442. uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
  443. output.WriteRawVarint32(tag);
  444. output.WriteRawVarint32(1);
  445. output.WriteRawBytes(new byte[] {0x80});
  446. output.Flush();
  447. ms.Position = 0;
  448. CodedInputStream input = CodedInputStream.CreateInstance(ms);
  449. uint testtag;
  450. string ignored;
  451. Assert.IsTrue(input.ReadTag(out testtag, out ignored));
  452. Assert.AreEqual(tag, testtag);
  453. string text = null;
  454. input.ReadString(ref text);
  455. Assert.AreEqual('\ufffd', text[0]);
  456. }
  457. /// <summary>
  458. /// A stream which limits the number of bytes it reads at a time.
  459. /// We use this to make sure that CodedInputStream doesn't screw up when
  460. /// reading in small blocks.
  461. /// </summary>
  462. private sealed class SmallBlockInputStream : MemoryStream
  463. {
  464. private readonly int blockSize;
  465. public SmallBlockInputStream(byte[] data, int blockSize)
  466. : base(data)
  467. {
  468. this.blockSize = blockSize;
  469. }
  470. public override int Read(byte[] buffer, int offset, int count)
  471. {
  472. return base.Read(buffer, offset, Math.Min(count, blockSize));
  473. }
  474. }
  475. enum TestNegEnum { None = 0, Value = -2 }
  476. [Test]
  477. public void TestNegativeEnum()
  478. {
  479. byte[] bytes = new byte[10] { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 };
  480. CodedInputStream input = CodedInputStream.CreateInstance(bytes);
  481. object unk;
  482. TestNegEnum val = TestNegEnum.None;
  483. Assert.IsTrue(input.ReadEnum(ref val, out unk));
  484. Assert.IsTrue(input.IsAtEnd);
  485. Assert.AreEqual(TestNegEnum.Value, val);
  486. }
  487. [Test]
  488. public void TestNegativeEnumPackedArray()
  489. {
  490. int arraySize = 1 + (10 * 5);
  491. int msgSize = 1 + 1 + arraySize;
  492. byte[] bytes = new byte[msgSize];
  493. CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
  494. output.WritePackedInt32Array(8, "", arraySize, new int[] { 0, -1, -2, -3, -4, -5 });
  495. Assert.AreEqual(0, output.SpaceLeft);
  496. CodedInputStream input = CodedInputStream.CreateInstance(bytes);
  497. uint tag;
  498. string name;
  499. Assert.IsTrue(input.ReadTag(out tag, out name));
  500. List<TestNegEnum> values = new List<TestNegEnum>();
  501. ICollection<object> unk;
  502. input.ReadEnumArray(tag, name, values, out unk);
  503. Assert.AreEqual(2, values.Count);
  504. Assert.AreEqual(TestNegEnum.None, values[0]);
  505. Assert.AreEqual(TestNegEnum.Value, values[1]);
  506. Assert.IsNotNull(unk);
  507. Assert.AreEqual(4, unk.Count);
  508. }
  509. [Test]
  510. public void TestNegativeEnumArray()
  511. {
  512. int arraySize = 1 + 1 + (11 * 5);
  513. int msgSize = arraySize;
  514. byte[] bytes = new byte[msgSize];
  515. CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
  516. output.WriteInt32Array(8, "", new int[] { 0, -1, -2, -3, -4, -5 });
  517. Assert.AreEqual(0, output.SpaceLeft);
  518. CodedInputStream input = CodedInputStream.CreateInstance(bytes);
  519. uint tag;
  520. string name;
  521. Assert.IsTrue(input.ReadTag(out tag, out name));
  522. List<TestNegEnum> values = new List<TestNegEnum>();
  523. ICollection<object> unk;
  524. input.ReadEnumArray(tag, name, values, out unk);
  525. Assert.AreEqual(2, values.Count);
  526. Assert.AreEqual(TestNegEnum.None, values[0]);
  527. Assert.AreEqual(TestNegEnum.Value, values[1]);
  528. Assert.IsNotNull(unk);
  529. Assert.AreEqual(4, unk.Count);
  530. }
  531. }
  532. }