1ffe3c632Sopenharmony_ci#region Copyright notice and license
2ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
3ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc.  All rights reserved.
4ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
5ffe3c632Sopenharmony_ci//
6ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
7ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
8ffe3c632Sopenharmony_ci// met:
9ffe3c632Sopenharmony_ci//
10ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
11ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
12ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
13ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
14ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
15ffe3c632Sopenharmony_ci// distribution.
16ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
17ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
18ffe3c632Sopenharmony_ci// this software without specific prior written permission.
19ffe3c632Sopenharmony_ci//
20ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31ffe3c632Sopenharmony_ci#endregion
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ciusing System;
34ffe3c632Sopenharmony_ciusing System.IO;
35ffe3c632Sopenharmony_ciusing Google.Protobuf.TestProtos;
36ffe3c632Sopenharmony_ciusing NUnit.Framework;
37ffe3c632Sopenharmony_ci
38ffe3c632Sopenharmony_cinamespace Google.Protobuf
39ffe3c632Sopenharmony_ci{
40ffe3c632Sopenharmony_ci    public class CodedInputStreamTest
41ffe3c632Sopenharmony_ci    {
42ffe3c632Sopenharmony_ci        /// <summary>
43ffe3c632Sopenharmony_ci        /// Helper to construct a byte array from a bunch of bytes.  The inputs are
44ffe3c632Sopenharmony_ci        /// actually ints so that I can use hex notation and not get stupid errors
45ffe3c632Sopenharmony_ci        /// about precision.
46ffe3c632Sopenharmony_ci        /// </summary>
47ffe3c632Sopenharmony_ci        private static byte[] Bytes(params int[] bytesAsInts)
48ffe3c632Sopenharmony_ci        {
49ffe3c632Sopenharmony_ci            byte[] bytes = new byte[bytesAsInts.Length];
50ffe3c632Sopenharmony_ci            for (int i = 0; i < bytesAsInts.Length; i++)
51ffe3c632Sopenharmony_ci            {
52ffe3c632Sopenharmony_ci                bytes[i] = (byte) bytesAsInts[i];
53ffe3c632Sopenharmony_ci            }
54ffe3c632Sopenharmony_ci            return bytes;
55ffe3c632Sopenharmony_ci        }
56ffe3c632Sopenharmony_ci
57ffe3c632Sopenharmony_ci        /// <summary>
58ffe3c632Sopenharmony_ci        /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64()
59ffe3c632Sopenharmony_ci        /// </summary>
60ffe3c632Sopenharmony_ci        private static void AssertReadVarint(byte[] data, ulong value)
61ffe3c632Sopenharmony_ci        {
62ffe3c632Sopenharmony_ci            CodedInputStream input = new CodedInputStream(data);
63ffe3c632Sopenharmony_ci            Assert.AreEqual((uint) value, input.ReadRawVarint32());
64ffe3c632Sopenharmony_ci
65ffe3c632Sopenharmony_ci            input = new CodedInputStream(data);
66ffe3c632Sopenharmony_ci            Assert.AreEqual(value, input.ReadRawVarint64());
67ffe3c632Sopenharmony_ci            Assert.IsTrue(input.IsAtEnd);
68ffe3c632Sopenharmony_ci
69ffe3c632Sopenharmony_ci            // Try different block sizes.
70ffe3c632Sopenharmony_ci            for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
71ffe3c632Sopenharmony_ci            {
72ffe3c632Sopenharmony_ci                input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize));
73ffe3c632Sopenharmony_ci                Assert.AreEqual((uint) value, input.ReadRawVarint32());
74ffe3c632Sopenharmony_ci
75ffe3c632Sopenharmony_ci                input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize));
76ffe3c632Sopenharmony_ci                Assert.AreEqual(value, input.ReadRawVarint64());
77ffe3c632Sopenharmony_ci                Assert.IsTrue(input.IsAtEnd);
78ffe3c632Sopenharmony_ci            }
79ffe3c632Sopenharmony_ci
80ffe3c632Sopenharmony_ci            // Try reading directly from a MemoryStream. We want to verify that it
81ffe3c632Sopenharmony_ci            // doesn't read past the end of the input, so write an extra byte - this
82ffe3c632Sopenharmony_ci            // lets us test the position at the end.
83ffe3c632Sopenharmony_ci            MemoryStream memoryStream = new MemoryStream();
84ffe3c632Sopenharmony_ci            memoryStream.Write(data, 0, data.Length);
85ffe3c632Sopenharmony_ci            memoryStream.WriteByte(0);
86ffe3c632Sopenharmony_ci            memoryStream.Position = 0;
87ffe3c632Sopenharmony_ci            Assert.AreEqual((uint) value, CodedInputStream.ReadRawVarint32(memoryStream));
88ffe3c632Sopenharmony_ci            Assert.AreEqual(data.Length, memoryStream.Position);
89ffe3c632Sopenharmony_ci        }
90ffe3c632Sopenharmony_ci
91ffe3c632Sopenharmony_ci        /// <summary>
92ffe3c632Sopenharmony_ci        /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and
93ffe3c632Sopenharmony_ci        /// expects them to fail with an InvalidProtocolBufferException whose
94ffe3c632Sopenharmony_ci        /// description matches the given one.
95ffe3c632Sopenharmony_ci        /// </summary>
96ffe3c632Sopenharmony_ci        private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data)
97ffe3c632Sopenharmony_ci        {
98ffe3c632Sopenharmony_ci            CodedInputStream input = new CodedInputStream(data);
99ffe3c632Sopenharmony_ci            var exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint32());
100ffe3c632Sopenharmony_ci            Assert.AreEqual(expected.Message, exception.Message);
101ffe3c632Sopenharmony_ci
102ffe3c632Sopenharmony_ci            input = new CodedInputStream(data);
103ffe3c632Sopenharmony_ci            exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint64());
104ffe3c632Sopenharmony_ci            Assert.AreEqual(expected.Message, exception.Message);
105ffe3c632Sopenharmony_ci
106ffe3c632Sopenharmony_ci            // Make sure we get the same error when reading directly from a Stream.
107ffe3c632Sopenharmony_ci            exception = Assert.Throws<InvalidProtocolBufferException>(() => CodedInputStream.ReadRawVarint32(new MemoryStream(data)));
108ffe3c632Sopenharmony_ci            Assert.AreEqual(expected.Message, exception.Message);
109ffe3c632Sopenharmony_ci        }
110ffe3c632Sopenharmony_ci
111ffe3c632Sopenharmony_ci        [Test]
112ffe3c632Sopenharmony_ci        public void ReadVarint()
113ffe3c632Sopenharmony_ci        {
114ffe3c632Sopenharmony_ci            AssertReadVarint(Bytes(0x00), 0);
115ffe3c632Sopenharmony_ci            AssertReadVarint(Bytes(0x01), 1);
116ffe3c632Sopenharmony_ci            AssertReadVarint(Bytes(0x7f), 127);
117ffe3c632Sopenharmony_ci            // 14882
118ffe3c632Sopenharmony_ci            AssertReadVarint(Bytes(0xa2, 0x74), (0x22 << 0) | (0x74 << 7));
119ffe3c632Sopenharmony_ci            // 2961488830
120ffe3c632Sopenharmony_ci            AssertReadVarint(Bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b),
121ffe3c632Sopenharmony_ci                             (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
122ffe3c632Sopenharmony_ci                             (0x0bL << 28));
123ffe3c632Sopenharmony_ci
124ffe3c632Sopenharmony_ci            // 64-bit
125ffe3c632Sopenharmony_ci            // 7256456126
126ffe3c632Sopenharmony_ci            AssertReadVarint(Bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b),
127ffe3c632Sopenharmony_ci                             (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
128ffe3c632Sopenharmony_ci                             (0x1bL << 28));
129ffe3c632Sopenharmony_ci            // 41256202580718336
130ffe3c632Sopenharmony_ci            AssertReadVarint(Bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49),
131ffe3c632Sopenharmony_ci                             (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
132ffe3c632Sopenharmony_ci                             (0x43L << 28) | (0x49L << 35) | (0x24L << 42) | (0x49L << 49));
133ffe3c632Sopenharmony_ci            // 11964378330978735131
134ffe3c632Sopenharmony_ci            AssertReadVarint(Bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01),
135ffe3c632Sopenharmony_ci                             (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
136ffe3c632Sopenharmony_ci                             (0x3bUL << 28) | (0x56UL << 35) | (0x00UL << 42) |
137ffe3c632Sopenharmony_ci                             (0x05UL << 49) | (0x26UL << 56) | (0x01UL << 63));
138ffe3c632Sopenharmony_ci
139ffe3c632Sopenharmony_ci            // Failures
140ffe3c632Sopenharmony_ci            AssertReadVarintFailure(
141ffe3c632Sopenharmony_ci                InvalidProtocolBufferException.MalformedVarint(),
142ffe3c632Sopenharmony_ci                Bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
143ffe3c632Sopenharmony_ci                      0x00));
144ffe3c632Sopenharmony_ci            AssertReadVarintFailure(
145ffe3c632Sopenharmony_ci                InvalidProtocolBufferException.TruncatedMessage(),
146ffe3c632Sopenharmony_ci                Bytes(0x80));
147ffe3c632Sopenharmony_ci        }
148ffe3c632Sopenharmony_ci
149ffe3c632Sopenharmony_ci        /// <summary>
150ffe3c632Sopenharmony_ci        /// Parses the given bytes using ReadRawLittleEndian32() and checks
151ffe3c632Sopenharmony_ci        /// that the result matches the given value.
152ffe3c632Sopenharmony_ci        /// </summary>
153ffe3c632Sopenharmony_ci        private static void AssertReadLittleEndian32(byte[] data, uint value)
154ffe3c632Sopenharmony_ci        {
155ffe3c632Sopenharmony_ci            CodedInputStream input = new CodedInputStream(data);
156ffe3c632Sopenharmony_ci            Assert.AreEqual(value, input.ReadRawLittleEndian32());
157ffe3c632Sopenharmony_ci            Assert.IsTrue(input.IsAtEnd);
158ffe3c632Sopenharmony_ci
159ffe3c632Sopenharmony_ci            // Try different block sizes.
160ffe3c632Sopenharmony_ci            for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
161ffe3c632Sopenharmony_ci            {
162ffe3c632Sopenharmony_ci                input = new CodedInputStream(
163ffe3c632Sopenharmony_ci                    new SmallBlockInputStream(data, blockSize));
164ffe3c632Sopenharmony_ci                Assert.AreEqual(value, input.ReadRawLittleEndian32());
165ffe3c632Sopenharmony_ci                Assert.IsTrue(input.IsAtEnd);
166ffe3c632Sopenharmony_ci            }
167ffe3c632Sopenharmony_ci        }
168ffe3c632Sopenharmony_ci
169ffe3c632Sopenharmony_ci        /// <summary>
170ffe3c632Sopenharmony_ci        /// Parses the given bytes using ReadRawLittleEndian64() and checks
171ffe3c632Sopenharmony_ci        /// that the result matches the given value.
172ffe3c632Sopenharmony_ci        /// </summary>
173ffe3c632Sopenharmony_ci        private static void AssertReadLittleEndian64(byte[] data, ulong value)
174ffe3c632Sopenharmony_ci        {
175ffe3c632Sopenharmony_ci            CodedInputStream input = new CodedInputStream(data);
176ffe3c632Sopenharmony_ci            Assert.AreEqual(value, input.ReadRawLittleEndian64());
177ffe3c632Sopenharmony_ci            Assert.IsTrue(input.IsAtEnd);
178ffe3c632Sopenharmony_ci
179ffe3c632Sopenharmony_ci            // Try different block sizes.
180ffe3c632Sopenharmony_ci            for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
181ffe3c632Sopenharmony_ci            {
182ffe3c632Sopenharmony_ci                input = new CodedInputStream(
183ffe3c632Sopenharmony_ci                    new SmallBlockInputStream(data, blockSize));
184ffe3c632Sopenharmony_ci                Assert.AreEqual(value, input.ReadRawLittleEndian64());
185ffe3c632Sopenharmony_ci                Assert.IsTrue(input.IsAtEnd);
186ffe3c632Sopenharmony_ci            }
187ffe3c632Sopenharmony_ci        }
188ffe3c632Sopenharmony_ci
189ffe3c632Sopenharmony_ci        [Test]
190ffe3c632Sopenharmony_ci        public void ReadLittleEndian()
191ffe3c632Sopenharmony_ci        {
192ffe3c632Sopenharmony_ci            AssertReadLittleEndian32(Bytes(0x78, 0x56, 0x34, 0x12), 0x12345678);
193ffe3c632Sopenharmony_ci            AssertReadLittleEndian32(Bytes(0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef0);
194ffe3c632Sopenharmony_ci
195ffe3c632Sopenharmony_ci            AssertReadLittleEndian64(Bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12),
196ffe3c632Sopenharmony_ci                                     0x123456789abcdef0L);
197ffe3c632Sopenharmony_ci            AssertReadLittleEndian64(
198ffe3c632Sopenharmony_ci                Bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef012345678UL);
199ffe3c632Sopenharmony_ci        }
200ffe3c632Sopenharmony_ci
201ffe3c632Sopenharmony_ci        [Test]
202ffe3c632Sopenharmony_ci        public void DecodeZigZag32()
203ffe3c632Sopenharmony_ci        {
204ffe3c632Sopenharmony_ci            Assert.AreEqual(0, ParsingPrimitives.DecodeZigZag32(0));
205ffe3c632Sopenharmony_ci            Assert.AreEqual(-1, ParsingPrimitives.DecodeZigZag32(1));
206ffe3c632Sopenharmony_ci            Assert.AreEqual(1, ParsingPrimitives.DecodeZigZag32(2));
207ffe3c632Sopenharmony_ci            Assert.AreEqual(-2, ParsingPrimitives.DecodeZigZag32(3));
208ffe3c632Sopenharmony_ci            Assert.AreEqual(0x3FFFFFFF, ParsingPrimitives.DecodeZigZag32(0x7FFFFFFE));
209ffe3c632Sopenharmony_ci            Assert.AreEqual(unchecked((int) 0xC0000000), ParsingPrimitives.DecodeZigZag32(0x7FFFFFFF));
210ffe3c632Sopenharmony_ci            Assert.AreEqual(0x7FFFFFFF, ParsingPrimitives.DecodeZigZag32(0xFFFFFFFE));
211ffe3c632Sopenharmony_ci            Assert.AreEqual(unchecked((int) 0x80000000), ParsingPrimitives.DecodeZigZag32(0xFFFFFFFF));
212ffe3c632Sopenharmony_ci        }
213ffe3c632Sopenharmony_ci
214ffe3c632Sopenharmony_ci        [Test]
215ffe3c632Sopenharmony_ci        public void DecodeZigZag64()
216ffe3c632Sopenharmony_ci        {
217ffe3c632Sopenharmony_ci            Assert.AreEqual(0, ParsingPrimitives.DecodeZigZag64(0));
218ffe3c632Sopenharmony_ci            Assert.AreEqual(-1, ParsingPrimitives.DecodeZigZag64(1));
219ffe3c632Sopenharmony_ci            Assert.AreEqual(1, ParsingPrimitives.DecodeZigZag64(2));
220ffe3c632Sopenharmony_ci            Assert.AreEqual(-2, ParsingPrimitives.DecodeZigZag64(3));
221ffe3c632Sopenharmony_ci            Assert.AreEqual(0x000000003FFFFFFFL, ParsingPrimitives.DecodeZigZag64(0x000000007FFFFFFEL));
222ffe3c632Sopenharmony_ci            Assert.AreEqual(unchecked((long) 0xFFFFFFFFC0000000L), ParsingPrimitives.DecodeZigZag64(0x000000007FFFFFFFL));
223ffe3c632Sopenharmony_ci            Assert.AreEqual(0x000000007FFFFFFFL, ParsingPrimitives.DecodeZigZag64(0x00000000FFFFFFFEL));
224ffe3c632Sopenharmony_ci            Assert.AreEqual(unchecked((long) 0xFFFFFFFF80000000L), ParsingPrimitives.DecodeZigZag64(0x00000000FFFFFFFFL));
225ffe3c632Sopenharmony_ci            Assert.AreEqual(0x7FFFFFFFFFFFFFFFL, ParsingPrimitives.DecodeZigZag64(0xFFFFFFFFFFFFFFFEL));
226ffe3c632Sopenharmony_ci            Assert.AreEqual(unchecked((long) 0x8000000000000000L), ParsingPrimitives.DecodeZigZag64(0xFFFFFFFFFFFFFFFFL));
227ffe3c632Sopenharmony_ci        }
228ffe3c632Sopenharmony_ci
229ffe3c632Sopenharmony_ci        [Test]
230ffe3c632Sopenharmony_ci        public void ReadWholeMessage_VaryingBlockSizes()
231ffe3c632Sopenharmony_ci        {
232ffe3c632Sopenharmony_ci            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();
233ffe3c632Sopenharmony_ci
234ffe3c632Sopenharmony_ci            byte[] rawBytes = message.ToByteArray();
235ffe3c632Sopenharmony_ci            Assert.AreEqual(rawBytes.Length, message.CalculateSize());
236ffe3c632Sopenharmony_ci            TestAllTypes message2 = TestAllTypes.Parser.ParseFrom(rawBytes);
237ffe3c632Sopenharmony_ci            Assert.AreEqual(message, message2);
238ffe3c632Sopenharmony_ci
239ffe3c632Sopenharmony_ci            // Try different block sizes.
240ffe3c632Sopenharmony_ci            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
241ffe3c632Sopenharmony_ci            {
242ffe3c632Sopenharmony_ci                message2 = TestAllTypes.Parser.ParseFrom(new SmallBlockInputStream(rawBytes, blockSize));
243ffe3c632Sopenharmony_ci                Assert.AreEqual(message, message2);
244ffe3c632Sopenharmony_ci            }
245ffe3c632Sopenharmony_ci        }
246ffe3c632Sopenharmony_ci
247ffe3c632Sopenharmony_ci        [Test]
248ffe3c632Sopenharmony_ci        public void ReadHugeBlob()
249ffe3c632Sopenharmony_ci        {
250ffe3c632Sopenharmony_ci            // Allocate and initialize a 1MB blob.
251ffe3c632Sopenharmony_ci            byte[] blob = new byte[1 << 20];
252ffe3c632Sopenharmony_ci            for (int i = 0; i < blob.Length; i++)
253ffe3c632Sopenharmony_ci            {
254ffe3c632Sopenharmony_ci                blob[i] = (byte) i;
255ffe3c632Sopenharmony_ci            }
256ffe3c632Sopenharmony_ci
257ffe3c632Sopenharmony_ci            // Make a message containing it.
258ffe3c632Sopenharmony_ci            var message = new TestAllTypes { SingleBytes = ByteString.CopyFrom(blob) };
259ffe3c632Sopenharmony_ci
260ffe3c632Sopenharmony_ci            // Serialize and parse it.  Make sure to parse from an InputStream, not
261ffe3c632Sopenharmony_ci            // directly from a ByteString, so that CodedInputStream uses buffered
262ffe3c632Sopenharmony_ci            // reading.
263ffe3c632Sopenharmony_ci            TestAllTypes message2 = TestAllTypes.Parser.ParseFrom(message.ToByteString());
264ffe3c632Sopenharmony_ci
265ffe3c632Sopenharmony_ci            Assert.AreEqual(message, message2);
266ffe3c632Sopenharmony_ci        }
267ffe3c632Sopenharmony_ci
268ffe3c632Sopenharmony_ci        [Test]
269ffe3c632Sopenharmony_ci        public void ReadMaliciouslyLargeBlob()
270ffe3c632Sopenharmony_ci        {
271ffe3c632Sopenharmony_ci            MemoryStream ms = new MemoryStream();
272ffe3c632Sopenharmony_ci            CodedOutputStream output = new CodedOutputStream(ms);
273ffe3c632Sopenharmony_ci
274ffe3c632Sopenharmony_ci            uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
275ffe3c632Sopenharmony_ci            output.WriteRawVarint32(tag);
276ffe3c632Sopenharmony_ci            output.WriteRawVarint32(0x7FFFFFFF);
277ffe3c632Sopenharmony_ci            output.WriteRawBytes(new byte[32]); // Pad with a few random bytes.
278ffe3c632Sopenharmony_ci            output.Flush();
279ffe3c632Sopenharmony_ci            ms.Position = 0;
280ffe3c632Sopenharmony_ci
281ffe3c632Sopenharmony_ci            CodedInputStream input = new CodedInputStream(ms);
282ffe3c632Sopenharmony_ci            Assert.AreEqual(tag, input.ReadTag());
283ffe3c632Sopenharmony_ci
284ffe3c632Sopenharmony_ci            Assert.Throws<InvalidProtocolBufferException>(() => input.ReadBytes());
285ffe3c632Sopenharmony_ci        }
286ffe3c632Sopenharmony_ci
287ffe3c632Sopenharmony_ci        internal static TestRecursiveMessage MakeRecursiveMessage(int depth)
288ffe3c632Sopenharmony_ci        {
289ffe3c632Sopenharmony_ci            if (depth == 0)
290ffe3c632Sopenharmony_ci            {
291ffe3c632Sopenharmony_ci                return new TestRecursiveMessage { I = 5 };
292ffe3c632Sopenharmony_ci            }
293ffe3c632Sopenharmony_ci            else
294ffe3c632Sopenharmony_ci            {
295ffe3c632Sopenharmony_ci                return new TestRecursiveMessage { A = MakeRecursiveMessage(depth - 1) };
296ffe3c632Sopenharmony_ci            }
297ffe3c632Sopenharmony_ci        }
298ffe3c632Sopenharmony_ci
299ffe3c632Sopenharmony_ci        internal static void AssertMessageDepth(TestRecursiveMessage message, int depth)
300ffe3c632Sopenharmony_ci        {
301ffe3c632Sopenharmony_ci            if (depth == 0)
302ffe3c632Sopenharmony_ci            {
303ffe3c632Sopenharmony_ci                Assert.IsNull(message.A);
304ffe3c632Sopenharmony_ci                Assert.AreEqual(5, message.I);
305ffe3c632Sopenharmony_ci            }
306ffe3c632Sopenharmony_ci            else
307ffe3c632Sopenharmony_ci            {
308ffe3c632Sopenharmony_ci                Assert.IsNotNull(message.A);
309ffe3c632Sopenharmony_ci                AssertMessageDepth(message.A, depth - 1);
310ffe3c632Sopenharmony_ci            }
311ffe3c632Sopenharmony_ci        }
312ffe3c632Sopenharmony_ci
313ffe3c632Sopenharmony_ci        [Test]
314ffe3c632Sopenharmony_ci        public void MaliciousRecursion()
315ffe3c632Sopenharmony_ci        {
316ffe3c632Sopenharmony_ci            ByteString atRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit).ToByteString();
317ffe3c632Sopenharmony_ci            ByteString beyondRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit + 1).ToByteString();
318ffe3c632Sopenharmony_ci
319ffe3c632Sopenharmony_ci            AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(atRecursiveLimit), CodedInputStream.DefaultRecursionLimit);
320ffe3c632Sopenharmony_ci
321ffe3c632Sopenharmony_ci            Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(beyondRecursiveLimit));
322ffe3c632Sopenharmony_ci
323ffe3c632Sopenharmony_ci            CodedInputStream input = CodedInputStream.CreateWithLimits(new MemoryStream(atRecursiveLimit.ToByteArray()), 1000000, CodedInputStream.DefaultRecursionLimit - 1);
324ffe3c632Sopenharmony_ci            Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(input));
325ffe3c632Sopenharmony_ci        }
326ffe3c632Sopenharmony_ci
327ffe3c632Sopenharmony_ci        [Test]
328ffe3c632Sopenharmony_ci        public void SizeLimit()
329ffe3c632Sopenharmony_ci        {
330ffe3c632Sopenharmony_ci            // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't
331ffe3c632Sopenharmony_ci            // apply to the latter case.
332ffe3c632Sopenharmony_ci            MemoryStream ms = new MemoryStream(SampleMessages.CreateFullTestAllTypes().ToByteArray());
333ffe3c632Sopenharmony_ci            CodedInputStream input = CodedInputStream.CreateWithLimits(ms, 16, 100);
334ffe3c632Sopenharmony_ci            Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseFrom(input));
335ffe3c632Sopenharmony_ci        }
336ffe3c632Sopenharmony_ci
337ffe3c632Sopenharmony_ci        /// <summary>
338ffe3c632Sopenharmony_ci        /// Tests that if we read an string that contains invalid UTF-8, no exception
339ffe3c632Sopenharmony_ci        /// is thrown.  Instead, the invalid bytes are replaced with the Unicode
340ffe3c632Sopenharmony_ci        /// "replacement character" U+FFFD.
341ffe3c632Sopenharmony_ci        /// </summary>
342ffe3c632Sopenharmony_ci        [Test]
343ffe3c632Sopenharmony_ci        public void ReadInvalidUtf8()
344ffe3c632Sopenharmony_ci        {
345ffe3c632Sopenharmony_ci            MemoryStream ms = new MemoryStream();
346ffe3c632Sopenharmony_ci            CodedOutputStream output = new CodedOutputStream(ms);
347ffe3c632Sopenharmony_ci
348ffe3c632Sopenharmony_ci            uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
349ffe3c632Sopenharmony_ci            output.WriteRawVarint32(tag);
350ffe3c632Sopenharmony_ci            output.WriteRawVarint32(1);
351ffe3c632Sopenharmony_ci            output.WriteRawBytes(new byte[] {0x80});
352ffe3c632Sopenharmony_ci            output.Flush();
353ffe3c632Sopenharmony_ci            ms.Position = 0;
354ffe3c632Sopenharmony_ci
355ffe3c632Sopenharmony_ci            CodedInputStream input = new CodedInputStream(ms);
356ffe3c632Sopenharmony_ci
357ffe3c632Sopenharmony_ci            Assert.AreEqual(tag, input.ReadTag());
358ffe3c632Sopenharmony_ci            string text = input.ReadString();
359ffe3c632Sopenharmony_ci            Assert.AreEqual('\ufffd', text[0]);
360ffe3c632Sopenharmony_ci        }
361ffe3c632Sopenharmony_ci
362ffe3c632Sopenharmony_ci        /// <summary>
363ffe3c632Sopenharmony_ci        /// A stream which limits the number of bytes it reads at a time.
364ffe3c632Sopenharmony_ci        /// We use this to make sure that CodedInputStream doesn't screw up when
365ffe3c632Sopenharmony_ci        /// reading in small blocks.
366ffe3c632Sopenharmony_ci        /// </summary>
367ffe3c632Sopenharmony_ci        private sealed class SmallBlockInputStream : MemoryStream
368ffe3c632Sopenharmony_ci        {
369ffe3c632Sopenharmony_ci            private readonly int blockSize;
370ffe3c632Sopenharmony_ci
371ffe3c632Sopenharmony_ci            public SmallBlockInputStream(byte[] data, int blockSize)
372ffe3c632Sopenharmony_ci                : base(data)
373ffe3c632Sopenharmony_ci            {
374ffe3c632Sopenharmony_ci                this.blockSize = blockSize;
375ffe3c632Sopenharmony_ci            }
376ffe3c632Sopenharmony_ci
377ffe3c632Sopenharmony_ci            public override int Read(byte[] buffer, int offset, int count)
378ffe3c632Sopenharmony_ci            {
379ffe3c632Sopenharmony_ci                return base.Read(buffer, offset, Math.Min(count, blockSize));
380ffe3c632Sopenharmony_ci            }
381ffe3c632Sopenharmony_ci        }
382ffe3c632Sopenharmony_ci
383ffe3c632Sopenharmony_ci        [Test]
384ffe3c632Sopenharmony_ci        public void TestNegativeEnum()
385ffe3c632Sopenharmony_ci        {
386ffe3c632Sopenharmony_ci            byte[] bytes = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 };
387ffe3c632Sopenharmony_ci            CodedInputStream input = new CodedInputStream(bytes);
388ffe3c632Sopenharmony_ci            Assert.AreEqual((int)SampleEnum.NegativeValue, input.ReadEnum());
389ffe3c632Sopenharmony_ci            Assert.IsTrue(input.IsAtEnd);
390ffe3c632Sopenharmony_ci        }
391ffe3c632Sopenharmony_ci
392ffe3c632Sopenharmony_ci        //Issue 71:	CodedInputStream.ReadBytes go to slow path unnecessarily
393ffe3c632Sopenharmony_ci        [Test]
394ffe3c632Sopenharmony_ci        public void TestSlowPathAvoidance()
395ffe3c632Sopenharmony_ci        {
396ffe3c632Sopenharmony_ci            using (var ms = new MemoryStream())
397ffe3c632Sopenharmony_ci            {
398ffe3c632Sopenharmony_ci                CodedOutputStream output = new CodedOutputStream(ms);
399ffe3c632Sopenharmony_ci                output.WriteTag(1, WireFormat.WireType.LengthDelimited);
400ffe3c632Sopenharmony_ci                output.WriteBytes(ByteString.CopyFrom(new byte[100]));
401ffe3c632Sopenharmony_ci                output.WriteTag(2, WireFormat.WireType.LengthDelimited);
402ffe3c632Sopenharmony_ci                output.WriteBytes(ByteString.CopyFrom(new byte[100]));
403ffe3c632Sopenharmony_ci                output.Flush();
404ffe3c632Sopenharmony_ci
405ffe3c632Sopenharmony_ci                ms.Position = 0;
406ffe3c632Sopenharmony_ci                CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2], 0, 0, false);
407ffe3c632Sopenharmony_ci
408ffe3c632Sopenharmony_ci                uint tag = input.ReadTag();
409ffe3c632Sopenharmony_ci                Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag));
410ffe3c632Sopenharmony_ci                Assert.AreEqual(100, input.ReadBytes().Length);
411ffe3c632Sopenharmony_ci
412ffe3c632Sopenharmony_ci                tag = input.ReadTag();
413ffe3c632Sopenharmony_ci                Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag));
414ffe3c632Sopenharmony_ci                Assert.AreEqual(100, input.ReadBytes().Length);
415ffe3c632Sopenharmony_ci            }
416ffe3c632Sopenharmony_ci        }
417ffe3c632Sopenharmony_ci
418ffe3c632Sopenharmony_ci        [Test]
419ffe3c632Sopenharmony_ci        public void Tag0Throws()
420ffe3c632Sopenharmony_ci        {
421ffe3c632Sopenharmony_ci            var input = new CodedInputStream(new byte[] { 0 });
422ffe3c632Sopenharmony_ci            Assert.Throws<InvalidProtocolBufferException>(() => input.ReadTag());
423ffe3c632Sopenharmony_ci        }
424ffe3c632Sopenharmony_ci
425ffe3c632Sopenharmony_ci        [Test]
426ffe3c632Sopenharmony_ci        public void SkipGroup()
427ffe3c632Sopenharmony_ci        {
428ffe3c632Sopenharmony_ci            // Create an output stream with a group in:
429ffe3c632Sopenharmony_ci            // Field 1: string "field 1"
430ffe3c632Sopenharmony_ci            // Field 2: group containing:
431ffe3c632Sopenharmony_ci            //   Field 1: fixed int32 value 100
432ffe3c632Sopenharmony_ci            //   Field 2: string "ignore me"
433ffe3c632Sopenharmony_ci            //   Field 3: nested group containing
434ffe3c632Sopenharmony_ci            //      Field 1: fixed int64 value 1000
435ffe3c632Sopenharmony_ci            // Field 3: string "field 3"
436ffe3c632Sopenharmony_ci            var stream = new MemoryStream();
437ffe3c632Sopenharmony_ci            var output = new CodedOutputStream(stream);
438ffe3c632Sopenharmony_ci            output.WriteTag(1, WireFormat.WireType.LengthDelimited);
439ffe3c632Sopenharmony_ci            output.WriteString("field 1");
440ffe3c632Sopenharmony_ci
441ffe3c632Sopenharmony_ci            // The outer group...
442ffe3c632Sopenharmony_ci            output.WriteTag(2, WireFormat.WireType.StartGroup);
443ffe3c632Sopenharmony_ci            output.WriteTag(1, WireFormat.WireType.Fixed32);
444ffe3c632Sopenharmony_ci            output.WriteFixed32(100);
445ffe3c632Sopenharmony_ci            output.WriteTag(2, WireFormat.WireType.LengthDelimited);
446ffe3c632Sopenharmony_ci            output.WriteString("ignore me");
447ffe3c632Sopenharmony_ci            // The nested group...
448ffe3c632Sopenharmony_ci            output.WriteTag(3, WireFormat.WireType.StartGroup);
449ffe3c632Sopenharmony_ci            output.WriteTag(1, WireFormat.WireType.Fixed64);
450ffe3c632Sopenharmony_ci            output.WriteFixed64(1000);
451ffe3c632Sopenharmony_ci            // Note: Not sure the field number is relevant for end group...
452ffe3c632Sopenharmony_ci            output.WriteTag(3, WireFormat.WireType.EndGroup);
453ffe3c632Sopenharmony_ci
454ffe3c632Sopenharmony_ci            // End the outer group
455ffe3c632Sopenharmony_ci            output.WriteTag(2, WireFormat.WireType.EndGroup);
456ffe3c632Sopenharmony_ci
457ffe3c632Sopenharmony_ci            output.WriteTag(3, WireFormat.WireType.LengthDelimited);
458ffe3c632Sopenharmony_ci            output.WriteString("field 3");
459ffe3c632Sopenharmony_ci            output.Flush();
460ffe3c632Sopenharmony_ci            stream.Position = 0;
461ffe3c632Sopenharmony_ci
462ffe3c632Sopenharmony_ci            // Now act like a generated client
463ffe3c632Sopenharmony_ci            var input = new CodedInputStream(stream);
464ffe3c632Sopenharmony_ci            Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag());
465ffe3c632Sopenharmony_ci            Assert.AreEqual("field 1", input.ReadString());
466ffe3c632Sopenharmony_ci            Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag());
467ffe3c632Sopenharmony_ci            input.SkipLastField(); // Should consume the whole group, including the nested one.
468ffe3c632Sopenharmony_ci            Assert.AreEqual(WireFormat.MakeTag(3, WireFormat.WireType.LengthDelimited), input.ReadTag());
469ffe3c632Sopenharmony_ci            Assert.AreEqual("field 3", input.ReadString());
470ffe3c632Sopenharmony_ci        }
471ffe3c632Sopenharmony_ci
472ffe3c632Sopenharmony_ci        [Test]
473ffe3c632Sopenharmony_ci        public void SkipGroup_WrongEndGroupTag()
474ffe3c632Sopenharmony_ci        {
475ffe3c632Sopenharmony_ci            // Create an output stream with:
476ffe3c632Sopenharmony_ci            // Field 1: string "field 1"
477ffe3c632Sopenharmony_ci            // Start group 2
478ffe3c632Sopenharmony_ci            //   Field 3: fixed int32
479ffe3c632Sopenharmony_ci            // End group 4 (should give an error)
480ffe3c632Sopenharmony_ci            var stream = new MemoryStream();
481ffe3c632Sopenharmony_ci            var output = new CodedOutputStream(stream);
482ffe3c632Sopenharmony_ci            output.WriteTag(1, WireFormat.WireType.LengthDelimited);
483ffe3c632Sopenharmony_ci            output.WriteString("field 1");
484ffe3c632Sopenharmony_ci
485ffe3c632Sopenharmony_ci            // The outer group...
486ffe3c632Sopenharmony_ci            output.WriteTag(2, WireFormat.WireType.StartGroup);
487ffe3c632Sopenharmony_ci            output.WriteTag(3, WireFormat.WireType.Fixed32);
488ffe3c632Sopenharmony_ci            output.WriteFixed32(100);
489ffe3c632Sopenharmony_ci            output.WriteTag(4, WireFormat.WireType.EndGroup);
490ffe3c632Sopenharmony_ci            output.Flush();
491ffe3c632Sopenharmony_ci            stream.Position = 0;
492ffe3c632Sopenharmony_ci
493ffe3c632Sopenharmony_ci            // Now act like a generated client
494ffe3c632Sopenharmony_ci            var input = new CodedInputStream(stream);
495ffe3c632Sopenharmony_ci            Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag());
496ffe3c632Sopenharmony_ci            Assert.AreEqual("field 1", input.ReadString());
497ffe3c632Sopenharmony_ci            Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag());
498ffe3c632Sopenharmony_ci            Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField);
499ffe3c632Sopenharmony_ci        }
500ffe3c632Sopenharmony_ci
501ffe3c632Sopenharmony_ci        [Test]
502ffe3c632Sopenharmony_ci        public void RogueEndGroupTag()
503ffe3c632Sopenharmony_ci        {
504ffe3c632Sopenharmony_ci            // If we have an end-group tag without a leading start-group tag, generated
505ffe3c632Sopenharmony_ci            // code will just call SkipLastField... so that should fail.
506ffe3c632Sopenharmony_ci
507ffe3c632Sopenharmony_ci            var stream = new MemoryStream();
508ffe3c632Sopenharmony_ci            var output = new CodedOutputStream(stream);
509ffe3c632Sopenharmony_ci            output.WriteTag(1, WireFormat.WireType.EndGroup);
510ffe3c632Sopenharmony_ci            output.Flush();
511ffe3c632Sopenharmony_ci            stream.Position = 0;
512ffe3c632Sopenharmony_ci
513ffe3c632Sopenharmony_ci            var input = new CodedInputStream(stream);
514ffe3c632Sopenharmony_ci            Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.EndGroup), input.ReadTag());
515ffe3c632Sopenharmony_ci            Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField);
516ffe3c632Sopenharmony_ci        }
517ffe3c632Sopenharmony_ci
518ffe3c632Sopenharmony_ci        [Test]
519ffe3c632Sopenharmony_ci        public void EndOfStreamReachedWhileSkippingGroup()
520ffe3c632Sopenharmony_ci        {
521ffe3c632Sopenharmony_ci            var stream = new MemoryStream();
522ffe3c632Sopenharmony_ci            var output = new CodedOutputStream(stream);
523ffe3c632Sopenharmony_ci            output.WriteTag(1, WireFormat.WireType.StartGroup);
524ffe3c632Sopenharmony_ci            output.WriteTag(2, WireFormat.WireType.StartGroup);
525ffe3c632Sopenharmony_ci            output.WriteTag(2, WireFormat.WireType.EndGroup);
526ffe3c632Sopenharmony_ci
527ffe3c632Sopenharmony_ci            output.Flush();
528ffe3c632Sopenharmony_ci            stream.Position = 0;
529ffe3c632Sopenharmony_ci
530ffe3c632Sopenharmony_ci            // Now act like a generated client
531ffe3c632Sopenharmony_ci            var input = new CodedInputStream(stream);
532ffe3c632Sopenharmony_ci            input.ReadTag();
533ffe3c632Sopenharmony_ci            Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField);
534ffe3c632Sopenharmony_ci        }
535ffe3c632Sopenharmony_ci
536ffe3c632Sopenharmony_ci        [Test]
537ffe3c632Sopenharmony_ci        public void RecursionLimitAppliedWhileSkippingGroup()
538ffe3c632Sopenharmony_ci        {
539ffe3c632Sopenharmony_ci            var stream = new MemoryStream();
540ffe3c632Sopenharmony_ci            var output = new CodedOutputStream(stream);
541ffe3c632Sopenharmony_ci            for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++)
542ffe3c632Sopenharmony_ci            {
543ffe3c632Sopenharmony_ci                output.WriteTag(1, WireFormat.WireType.StartGroup);
544ffe3c632Sopenharmony_ci            }
545ffe3c632Sopenharmony_ci            for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++)
546ffe3c632Sopenharmony_ci            {
547ffe3c632Sopenharmony_ci                output.WriteTag(1, WireFormat.WireType.EndGroup);
548ffe3c632Sopenharmony_ci            }
549ffe3c632Sopenharmony_ci            output.Flush();
550ffe3c632Sopenharmony_ci            stream.Position = 0;
551ffe3c632Sopenharmony_ci
552ffe3c632Sopenharmony_ci            // Now act like a generated client
553ffe3c632Sopenharmony_ci            var input = new CodedInputStream(stream);
554ffe3c632Sopenharmony_ci            Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.StartGroup), input.ReadTag());
555ffe3c632Sopenharmony_ci            Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField);
556ffe3c632Sopenharmony_ci        }
557ffe3c632Sopenharmony_ci
558ffe3c632Sopenharmony_ci        [Test]
559ffe3c632Sopenharmony_ci        public void Construction_Invalid()
560ffe3c632Sopenharmony_ci        {
561ffe3c632Sopenharmony_ci            Assert.Throws<ArgumentNullException>(() => new CodedInputStream((byte[]) null));
562ffe3c632Sopenharmony_ci            Assert.Throws<ArgumentNullException>(() => new CodedInputStream(null, 0, 0));
563ffe3c632Sopenharmony_ci            Assert.Throws<ArgumentNullException>(() => new CodedInputStream((Stream) null));
564ffe3c632Sopenharmony_ci            Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStream(new byte[10], 100, 0));
565ffe3c632Sopenharmony_ci            Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStream(new byte[10], 5, 10));
566ffe3c632Sopenharmony_ci        }
567ffe3c632Sopenharmony_ci
568ffe3c632Sopenharmony_ci        [Test]
569ffe3c632Sopenharmony_ci        public void CreateWithLimits_InvalidLimits()
570ffe3c632Sopenharmony_ci        {
571ffe3c632Sopenharmony_ci            var stream = new MemoryStream();
572ffe3c632Sopenharmony_ci            Assert.Throws<ArgumentOutOfRangeException>(() => CodedInputStream.CreateWithLimits(stream, 0, 1));
573ffe3c632Sopenharmony_ci            Assert.Throws<ArgumentOutOfRangeException>(() => CodedInputStream.CreateWithLimits(stream, 1, 0));
574ffe3c632Sopenharmony_ci        }
575ffe3c632Sopenharmony_ci
576ffe3c632Sopenharmony_ci        [Test]
577ffe3c632Sopenharmony_ci        public void Dispose_DisposesUnderlyingStream()
578ffe3c632Sopenharmony_ci        {
579ffe3c632Sopenharmony_ci            var memoryStream = new MemoryStream();
580ffe3c632Sopenharmony_ci            Assert.IsTrue(memoryStream.CanRead);
581ffe3c632Sopenharmony_ci            using (var cis = new CodedInputStream(memoryStream))
582ffe3c632Sopenharmony_ci            {
583ffe3c632Sopenharmony_ci            }
584ffe3c632Sopenharmony_ci            Assert.IsFalse(memoryStream.CanRead); // Disposed
585ffe3c632Sopenharmony_ci        }
586ffe3c632Sopenharmony_ci
587ffe3c632Sopenharmony_ci        [Test]
588ffe3c632Sopenharmony_ci        public void Dispose_WithLeaveOpen()
589ffe3c632Sopenharmony_ci        {
590ffe3c632Sopenharmony_ci            var memoryStream = new MemoryStream();
591ffe3c632Sopenharmony_ci            Assert.IsTrue(memoryStream.CanRead);
592ffe3c632Sopenharmony_ci            using (var cis = new CodedInputStream(memoryStream, true))
593ffe3c632Sopenharmony_ci            {
594ffe3c632Sopenharmony_ci            }
595ffe3c632Sopenharmony_ci            Assert.IsTrue(memoryStream.CanRead); // We left the stream open
596ffe3c632Sopenharmony_ci        }
597ffe3c632Sopenharmony_ci    }
598ffe3c632Sopenharmony_ci}