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