OLD | NEW |
1 #region Copyright notice and license | 1 #region Copyright notice and license |
2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
3 // Copyright 2008 Google Inc. All rights reserved. | 3 // Copyright 2008 Google Inc. All rights reserved. |
4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
5 // | 5 // |
6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
8 // met: | 8 // met: |
9 // | 9 // |
10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 var input = new CodedInputStream(stream); | 463 var input = new CodedInputStream(stream); |
464 Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDeli
mited), input.ReadTag()); | 464 Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDeli
mited), input.ReadTag()); |
465 Assert.AreEqual("field 1", input.ReadString()); | 465 Assert.AreEqual("field 1", input.ReadString()); |
466 Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup
), input.ReadTag()); | 466 Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup
), input.ReadTag()); |
467 input.SkipLastField(); // Should consume the whole group, including
the nested one. | 467 input.SkipLastField(); // Should consume the whole group, including
the nested one. |
468 Assert.AreEqual(WireFormat.MakeTag(3, WireFormat.WireType.LengthDeli
mited), input.ReadTag()); | 468 Assert.AreEqual(WireFormat.MakeTag(3, WireFormat.WireType.LengthDeli
mited), input.ReadTag()); |
469 Assert.AreEqual("field 3", input.ReadString()); | 469 Assert.AreEqual("field 3", input.ReadString()); |
470 } | 470 } |
471 | 471 |
472 [Test] | 472 [Test] |
| 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.LengthDeli
mited), 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] |
| 502 public void RogueEndGroupTag() |
| 503 { |
| 504 // If we have an end-group tag without a leading start-group tag, ge
nerated |
| 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] |
473 public void EndOfStreamReachedWhileSkippingGroup() | 519 public void EndOfStreamReachedWhileSkippingGroup() |
474 { | 520 { |
475 var stream = new MemoryStream(); | 521 var stream = new MemoryStream(); |
476 var output = new CodedOutputStream(stream); | 522 var output = new CodedOutputStream(stream); |
477 output.WriteTag(1, WireFormat.WireType.StartGroup); | 523 output.WriteTag(1, WireFormat.WireType.StartGroup); |
478 output.WriteTag(2, WireFormat.WireType.StartGroup); | 524 output.WriteTag(2, WireFormat.WireType.StartGroup); |
479 output.WriteTag(2, WireFormat.WireType.EndGroup); | 525 output.WriteTag(2, WireFormat.WireType.EndGroup); |
480 | 526 |
481 output.Flush(); | 527 output.Flush(); |
482 stream.Position = 0; | 528 stream.Position = 0; |
483 | 529 |
484 // Now act like a generated client | 530 // Now act like a generated client |
485 var input = new CodedInputStream(stream); | 531 var input = new CodedInputStream(stream); |
486 input.ReadTag(); | 532 input.ReadTag(); |
487 Assert.Throws<InvalidProtocolBufferException>(() => input.SkipLastFi
eld()); | 533 Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField); |
488 } | 534 } |
489 | 535 |
490 [Test] | 536 [Test] |
491 public void RecursionLimitAppliedWhileSkippingGroup() | 537 public void RecursionLimitAppliedWhileSkippingGroup() |
492 { | 538 { |
493 var stream = new MemoryStream(); | 539 var stream = new MemoryStream(); |
494 var output = new CodedOutputStream(stream); | 540 var output = new CodedOutputStream(stream); |
495 for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) | 541 for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) |
496 { | 542 { |
497 output.WriteTag(1, WireFormat.WireType.StartGroup); | 543 output.WriteTag(1, WireFormat.WireType.StartGroup); |
498 } | 544 } |
499 for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) | 545 for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) |
500 { | 546 { |
501 output.WriteTag(1, WireFormat.WireType.EndGroup); | 547 output.WriteTag(1, WireFormat.WireType.EndGroup); |
502 } | 548 } |
503 output.Flush(); | 549 output.Flush(); |
504 stream.Position = 0; | 550 stream.Position = 0; |
505 | 551 |
506 // Now act like a generated client | 552 // Now act like a generated client |
507 var input = new CodedInputStream(stream); | 553 var input = new CodedInputStream(stream); |
508 Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.StartGroup
), input.ReadTag()); | 554 Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.StartGroup
), input.ReadTag()); |
509 Assert.Throws<InvalidProtocolBufferException>(() => input.SkipLastFi
eld()); | 555 Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField); |
510 } | 556 } |
511 | 557 |
512 [Test] | 558 [Test] |
513 public void Construction_Invalid() | 559 public void Construction_Invalid() |
514 { | 560 { |
515 Assert.Throws<ArgumentNullException>(() => new CodedInputStream((byt
e[]) null)); | 561 Assert.Throws<ArgumentNullException>(() => new CodedInputStream((byt
e[]) null)); |
516 Assert.Throws<ArgumentNullException>(() => new CodedInputStream(null
, 0, 0)); | 562 Assert.Throws<ArgumentNullException>(() => new CodedInputStream(null
, 0, 0)); |
517 Assert.Throws<ArgumentNullException>(() => new CodedInputStream((Str
eam) null)); | 563 Assert.Throws<ArgumentNullException>(() => new CodedInputStream((Str
eam) null)); |
518 Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStrea
m(new byte[10], 100, 0)); | 564 Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStrea
m(new byte[10], 100, 0)); |
519 Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStrea
m(new byte[10], 5, 10)); | 565 Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStrea
m(new byte[10], 5, 10)); |
520 } | 566 } |
521 | 567 |
522 [Test] | 568 [Test] |
523 public void CreateWithLimits_InvalidLimits() | 569 public void CreateWithLimits_InvalidLimits() |
524 { | 570 { |
525 var stream = new MemoryStream(); | 571 var stream = new MemoryStream(); |
526 Assert.Throws<ArgumentOutOfRangeException>(() => CodedInputStream.Cr
eateWithLimits(stream, 0, 1)); | 572 Assert.Throws<ArgumentOutOfRangeException>(() => CodedInputStream.Cr
eateWithLimits(stream, 0, 1)); |
527 Assert.Throws<ArgumentOutOfRangeException>(() => CodedInputStream.Cr
eateWithLimits(stream, 1, 0)); | 573 Assert.Throws<ArgumentOutOfRangeException>(() => CodedInputStream.Cr
eateWithLimits(stream, 1, 0)); |
528 } | 574 } |
| 575 |
| 576 [Test] |
| 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] |
| 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 } |
529 } | 597 } |
530 } | 598 } |
OLD | NEW |