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