diff options
Diffstat (limited to 'csharp/src/Google.Protobuf.Test')
7 files changed, 105 insertions, 8 deletions
diff --git a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs index 2b6265c1..6fdd1066 100644 --- a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs +++ b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs @@ -36,6 +36,8 @@ using Google.Protobuf.TestProtos; using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;
+using System.Linq;
+using Google.Protobuf.WellKnownTypes;
namespace Google.Protobuf
{
@@ -257,7 +259,7 @@ namespace Google.Protobuf output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited);
var nestedMessage = new ForeignMessage { C = 20 };
// Size of the entry (tag, size written by WriteMessage, data written by WriteMessage)
- output.WriteRawVarint32((uint)(nestedMessage.CalculateSize() + 3));
+ output.WriteLength(2 + nestedMessage.CalculateSize());
output.WriteTag(2, WireFormat.WireType.LengthDelimited);
output.WriteMessage(nestedMessage);
output.Flush();
@@ -281,7 +283,7 @@ namespace Google.Protobuf // Each field can be represented in a single byte, with a single byte tag.
// Total message size: 6 bytes.
- output.WriteRawVarint32(6);
+ output.WriteLength(6);
output.WriteTag(1, WireFormat.WireType.Varint);
output.WriteInt32(key);
output.WriteTag(2, WireFormat.WireType.Varint);
@@ -307,7 +309,7 @@ namespace Google.Protobuf // Each field can be represented in a single byte, with a single byte tag.
// Total message size: 4 bytes.
- output.WriteRawVarint32(4);
+ output.WriteLength(4);
output.WriteTag(2, WireFormat.WireType.Varint);
output.WriteInt32(value);
output.WriteTag(1, WireFormat.WireType.Varint);
@@ -333,7 +335,7 @@ namespace Google.Protobuf var key1 = 10;
var value1 = 20;
output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
- output.WriteRawVarint32(4);
+ output.WriteLength(4);
output.WriteTag(1, WireFormat.WireType.Varint);
output.WriteInt32(key1);
output.WriteTag(2, WireFormat.WireType.Varint);
@@ -343,7 +345,7 @@ namespace Google.Protobuf var key2 = "a";
var value2 = "b";
output.WriteTag(TestMap.MapStringStringFieldNumber, WireFormat.WireType.LengthDelimited);
- output.WriteRawVarint32(6); // 3 bytes per entry: tag, size, character
+ output.WriteLength(6); // 3 bytes per entry: tag, size, character
output.WriteTag(1, WireFormat.WireType.LengthDelimited);
output.WriteString(key2);
output.WriteTag(2, WireFormat.WireType.LengthDelimited);
@@ -353,7 +355,7 @@ namespace Google.Protobuf var key3 = 15;
var value3 = 25;
output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
- output.WriteRawVarint32(4);
+ output.WriteLength(4);
output.WriteTag(1, WireFormat.WireType.Varint);
output.WriteInt32(key3);
output.WriteTag(2, WireFormat.WireType.Varint);
@@ -381,7 +383,7 @@ namespace Google.Protobuf // First entry
output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
- output.WriteRawVarint32(4);
+ output.WriteLength(4);
output.WriteTag(1, WireFormat.WireType.Varint);
output.WriteInt32(key);
output.WriteTag(2, WireFormat.WireType.Varint);
@@ -389,7 +391,7 @@ namespace Google.Protobuf // Second entry - same key, different value
output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
- output.WriteRawVarint32(4);
+ output.WriteLength(4);
output.WriteTag(1, WireFormat.WireType.Varint);
output.WriteInt32(key);
output.WriteTag(2, WireFormat.WireType.Varint);
@@ -590,5 +592,42 @@ namespace Google.Protobuf Assert.AreEqual(message, message2);
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
}
+
+ [Test]
+ public void IgnoreUnknownFields_RealDataStillRead()
+ {
+ var message = SampleMessages.CreateFullTestAllTypes();
+ var stream = new MemoryStream();
+ var output = new CodedOutputStream(stream);
+ var unusedFieldNumber = 23456;
+ Assert.IsFalse(TestAllTypes.Descriptor.Fields.InDeclarationOrder().Select(x => x.FieldNumber).Contains(unusedFieldNumber));
+ output.WriteTag(unusedFieldNumber, WireFormat.WireType.LengthDelimited);
+ output.WriteString("ignore me");
+ message.WriteTo(output);
+ output.Flush();
+
+ stream.Position = 0;
+ var parsed = TestAllTypes.Parser.ParseFrom(stream);
+ Assert.AreEqual(message, parsed);
+ }
+
+ [Test]
+ public void IgnoreUnknownFields_AllTypes()
+ {
+ // Simple way of ensuring we can skip all kinds of fields.
+ var data = SampleMessages.CreateFullTestAllTypes().ToByteArray();
+ var empty = Empty.Parser.ParseFrom(data);
+ Assert.AreEqual(new Empty(), empty);
+ }
+
+ // This was originally seen as a conformance test failure.
+ [Test]
+ public void TruncatedMessageFieldThrows()
+ {
+ // 130, 3 is the message tag
+ // 1 is the data length - but there's no data.
+ var data = new byte[] { 130, 3, 1 };
+ Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseFrom(data));
+ }
}
}
diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/MapUnittestProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/MapUnittestProto3.cs index aac30ceb..db28213c 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/MapUnittestProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/MapUnittestProto3.cs @@ -481,6 +481,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { mapInt32Int32_.AddEntriesFrom(input, _map_mapInt32Int32_codec); @@ -654,6 +655,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { if (testMap_ == null) { @@ -755,6 +757,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { mapInt32Message_.AddEntriesFrom(input, _map_mapInt32Message_codec); @@ -867,6 +870,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { map1_.AddEntriesFrom(input, _map_map1_codec); @@ -1165,6 +1169,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { mapInt32Int32_.AddEntriesFrom(input, _map_mapInt32Int32_codec); @@ -1319,6 +1324,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { type_.AddEntriesFrom(input, _map_type_codec); @@ -1427,6 +1433,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { entry_.AddEntriesFrom(input, _map_entry_codec); diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportProto3.cs index fae10957..ae6e3a3f 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportProto3.cs @@ -144,6 +144,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { D = input.ReadInt32(); diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportPublicProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportPublicProto3.cs index 17689f43..103ea8bb 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportPublicProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportPublicProto3.cs @@ -130,6 +130,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { E = input.ReadInt32(); diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestIssues.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestIssues.cs index 8c0dc4e9..4e154456 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestIssues.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestIssues.cs @@ -147,6 +147,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -228,6 +229,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -309,6 +311,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -449,6 +452,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { value_ = (global::UnitTest.Issues.TestProtos.NegativeEnum) input.ReadEnum(); @@ -543,6 +547,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -740,6 +745,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { PrimitiveValue = input.ReadInt32(); @@ -871,6 +877,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { Item = input.ReadInt32(); @@ -999,6 +1006,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { Types_ = input.ReadInt32(); @@ -1088,6 +1096,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -1357,6 +1366,7 @@ namespace UnitTest.Issues.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { PlainString = input.ReadString(); diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs index e360ac15..9b4501c5 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs @@ -1216,6 +1216,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { SingleInt32 = input.ReadInt32(); @@ -1552,6 +1553,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { Bb = input.ReadInt32(); @@ -1705,6 +1707,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { if (child_ == null) { @@ -1826,6 +1829,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { DeprecatedInt32 = input.ReadInt32(); @@ -1932,6 +1936,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { C = input.ReadInt32(); @@ -2016,6 +2021,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -2121,6 +2127,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { if (foreignNested_ == null) { @@ -2252,6 +2259,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { A = input.ReadInt32(); @@ -2387,6 +2395,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { if (a_ == null) { @@ -2503,6 +2512,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { if (bb_ == null) { @@ -2637,6 +2647,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { if (a_ == null) { @@ -2875,6 +2886,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { PrimitiveField = input.ReadInt32(); @@ -3083,6 +3095,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { MyInt = input.ReadInt64(); @@ -3227,6 +3240,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { Bb = input.ReadInt32(); @@ -3342,6 +3356,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { sparseEnum_ = (global::Google.Protobuf.TestProtos.TestSparseEnum) input.ReadEnum(); @@ -3448,6 +3463,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { Data = input.ReadString(); @@ -3546,6 +3562,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { data_.AddEntriesFrom(input, _repeated_data_codec); @@ -3652,6 +3669,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { Data = input.ReadBytes(); @@ -3758,6 +3776,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { Data = input.ReadBytes(); @@ -3864,6 +3883,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { Data = input.ReadInt32(); @@ -3970,6 +3990,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { Data = input.ReadUInt32(); @@ -4076,6 +4097,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { Data = input.ReadInt64(); @@ -4182,6 +4204,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { Data = input.ReadUInt64(); @@ -4288,6 +4311,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { Data = input.ReadBool(); @@ -4467,6 +4491,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 8: { FooInt = input.ReadInt32(); @@ -4760,6 +4785,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 722: case 720: { @@ -5106,6 +5132,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 722: case 720: { @@ -5340,6 +5367,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 98: case 101: { @@ -5472,6 +5500,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { A = input.ReadString(); @@ -5556,6 +5585,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -5636,6 +5666,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -5716,6 +5747,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -5796,6 +5828,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -5876,6 +5909,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } @@ -5956,6 +5990,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; } } diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestWellKnownTypes.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestWellKnownTypes.cs index f18c5f1b..b05c80c1 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestWellKnownTypes.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestWellKnownTypes.cs @@ -684,6 +684,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { if (anyField_ == null) { @@ -1142,6 +1143,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { anyField_.AddEntriesFrom(input, _repeated_anyField_codec); @@ -1764,6 +1766,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { global::Google.Protobuf.WellKnownTypes.Any subBuilder = new global::Google.Protobuf.WellKnownTypes.Any(); @@ -2213,6 +2216,7 @@ namespace Google.Protobuf.TestProtos { if (pb::WireFormat.IsEndGroupTag(tag)) { return; } + input.ConsumeLastField(); break; case 10: { anyField_.AddEntriesFrom(input, _map_anyField_codec); |