static ByteString EncodeObject (object value, MemoryStream buffer, CodedOutputStream stream) { buffer.SetLength (0); if (value == null) { stream.WriteUInt64 (0); } else if (value is Enum) { stream.WriteInt32 ((int)value); } else { Type type = value.GetType (); switch (Type.GetTypeCode (type)) { case TypeCode.Int32: stream.WriteInt32 ((int)value); break; case TypeCode.Int64: stream.WriteInt64 ((long)value); break; case TypeCode.UInt32: stream.WriteUInt32 ((uint)value); break; case TypeCode.UInt64: stream.WriteUInt64 ((ulong)value); break; case TypeCode.Single: stream.WriteFloat ((float)value); break; case TypeCode.Double: stream.WriteDouble ((double)value); break; case TypeCode.Boolean: stream.WriteBool ((bool)value); break; case TypeCode.String: stream.WriteString ((string)value); break; default: if (type == typeof(byte[])) stream.WriteBytes (ByteString.CopyFrom ((byte[])value)); else if (TypeUtils.IsAClassType (type)) stream.WriteUInt64 (ObjectStore.Instance.AddInstance (value)); else if (TypeUtils.IsAMessageType (type)) WriteMessage (value, stream); else if (TypeUtils.IsAListCollectionType (type)) WriteList (value, stream); else if (TypeUtils.IsADictionaryCollectionType (type)) WriteDictionary (value, stream); else if (TypeUtils.IsASetCollectionType (type)) WriteSet (value, stream); else if (TypeUtils.IsATupleCollectionType (type)) WriteTuple (value, stream); else throw new ArgumentException (type + " is not a serializable type"); break; } } stream.Flush (); return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length); }
/// <summary> /// Convert class ByteString /// </summary> /// <param name="msg"></param> /// <returns></returns> public static Google.Protobuf.ByteString Marshal(Google.Protobuf.IMessage msg) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { var s = new Google.Protobuf.CodedOutputStream(ms); msg.WriteTo(s); s.Flush(); var str = Google.Protobuf.ByteString.CopyFrom(ms.ToArray()); return(str); } }
/// <summary> /// Convert class ByteString /// </summary> /// <param name="msg"></param> /// <returns></returns> public static byte[] MarshalByte(Google.Protobuf.IMessage msg) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { var s = new Google.Protobuf.CodedOutputStream(ms); msg.WriteTo(s); s.Flush(); return(ms.ToArray()); } }
public void SendReq(GameState request, out String response) { byte[] bytes = new byte[request.CalculateSize()]; Google.Protobuf.CodedOutputStream codedOutputStream = new Google.Protobuf.CodedOutputStream(bytes); request.WriteTo(codedOutputStream); codedOutputStream.Flush(); codedOutputStream.CheckNoSpaceLeft(); networkStream.Write(bytes, 0, bytes.Length); networkStream.Flush(); byte[] toRead = new byte[tcpClient.ReceiveBufferSize]; int bytesRead = networkStream.Read(toRead, 0, tcpClient.ReceiveBufferSize); response = Encoding.UTF8.GetString(toRead, 0, bytesRead); }
public void ExtraEndGroupThrows() { var message = SampleMessages.CreateFullTestAllTypes(); var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.WireType.Fixed32); output.WriteFixed32(123); output.WriteTag(100, WireFormat.WireType.EndGroup); output.Flush(); stream.Position = 0; Assert.Throws <IOException>(() => TestAllTypes.Parser.ParseFrom(stream)); }
public void ExtraEndGroupThrows() { var message = SampleMessages.CreateFullTestAllTypes(); var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.WireType.Fixed32); output.WriteFixed32(123); output.WriteTag(100, WireFormat.WireType.EndGroup); output.Flush(); stream.Position = 0; MessageParsingHelpers.AssertReadingMessageThrows <TestAllTypes, InvalidProtocolBufferException>(TestAllTypes.Parser, stream.ToArray()); }
public void SendReq(GameState gameState, out string recievedMessage) { byte[] bytes = new byte[gameState.CalculateSize()]; Google.Protobuf.CodedOutputStream codedOutputStream = new Google.Protobuf.CodedOutputStream(bytes); gameState.WriteTo(codedOutputStream); codedOutputStream.Flush(); client.SendFrame(bytes, false); //recievedMessage = client.ReceiveFrameString(); if (!client.TryReceiveFrameString(new TimeSpan(0, 0, 1), out recievedMessage)) { recievedMessage = ""; UnityEditor.EditorApplication.isPlaying = false; } }
/// <summary> /// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and /// checks that the result matches the given bytes /// </summary> private static void AssertWriteVarint(byte[] data, ulong value) { // Only do 32-bit write if the value fits in 32 bits. if ((value >> 32) == 0) { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput); output.WriteRawVarint32((uint)value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); // Also try computing size. Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint)value)); } { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput); output.WriteRawVarint64(value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); // Also try computing size. Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value)); } // Try different buffer sizes. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) { // Only do 32-bit write if the value fits in 32 bits. if ((value >> 32) == 0) { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput, bufferSize); output.WriteRawVarint32((uint)value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); } { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput, bufferSize); output.WriteRawVarint64(value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); } } }
/// <summary> /// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and /// checks that the result matches the given bytes /// </summary> private static void AssertWriteVarint(byte[] data, ulong value) { // Only do 32-bit write if the value fits in 32 bits. if ((value >> 32) == 0) { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput); output.WriteRawVarint32((uint) value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); // Also try computing size. Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint) value)); } { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput); output.WriteRawVarint64(value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); // Also try computing size. Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value)); } // Try different buffer sizes. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) { // Only do 32-bit write if the value fits in 32 bits. if ((value >> 32) == 0) { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput, bufferSize); output.WriteRawVarint32((uint) value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); } { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput, bufferSize); output.WriteRawVarint64(value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); } } }
public void WriteWholeMessage_VaryingBlockSizes() { TestAllTypes message = SampleMessages.CreateFullTestAllTypes(); byte[] rawBytes = message.ToByteArray(); // Try different block sizes. for (int blockSize = 1; blockSize < 256; blockSize *= 2) { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput, blockSize); message.WriteTo(output); output.Flush(); Assert.AreEqual(rawBytes, rawOutput.ToArray()); } }
public void SkipGroup() { // Create an output stream with a group in: // Field 1: string "field 1" // Field 2: group containing: // Field 1: fixed int32 value 100 // Field 2: string "ignore me" // Field 3: nested group containing // Field 1: fixed int64 value 1000 // Field 3: string "field 3" var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(1, WireFormat.WireType.LengthDelimited); output.WriteString("field 1"); // The outer group... output.WriteTag(2, WireFormat.WireType.StartGroup); output.WriteTag(1, WireFormat.WireType.Fixed32); output.WriteFixed32(100); output.WriteTag(2, WireFormat.WireType.LengthDelimited); output.WriteString("ignore me"); // The nested group... output.WriteTag(3, WireFormat.WireType.StartGroup); output.WriteTag(1, WireFormat.WireType.Fixed64); output.WriteFixed64(1000); // Note: Not sure the field number is relevant for end group... output.WriteTag(3, WireFormat.WireType.EndGroup); // End the outer group output.WriteTag(2, WireFormat.WireType.EndGroup); output.WriteTag(3, WireFormat.WireType.LengthDelimited); output.WriteString("field 3"); output.Flush(); stream.Position = 0; // Now act like a generated client var input = new CodedInputStream(stream); Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag()); Assert.AreEqual("field 1", input.ReadString()); Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag()); input.SkipLastField(); // Should consume the whole group, including the nested one. Assert.AreEqual(WireFormat.MakeTag(3, WireFormat.WireType.LengthDelimited), input.ReadTag()); Assert.AreEqual("field 3", input.ReadString()); }
public void ReadNegativeSizedBytesThrowsInvalidProtocolBufferException() { MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); output.WriteRawVarint32(tag); output.WriteLength(-1); output.Flush(); ms.Position = 0; CodedInputStream input = new CodedInputStream(ms); Assert.AreEqual(tag, input.ReadTag()); Assert.Throws <InvalidProtocolBufferException>(() => input.ReadBytes()); }
public void RogueEndGroupTag() { // If we have an end-group tag without a leading start-group tag, generated // code will just call SkipLastField... so that should fail. var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(1, WireFormat.WireType.EndGroup); output.Flush(); stream.Position = 0; var input = new CodedInputStream(stream); Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.EndGroup), input.ReadTag()); Assert.Throws <InvalidProtocolBufferException>(input.SkipLastField); }
public void TestReadInvalidWireTypeThrowsInvalidProtocolBufferException() { MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); uint tag = WireFormat.MakeTag(1, (WireFormat.WireType) 6); output.WriteRawVarint32(tag); output.WriteLength(-1); output.Flush(); ms.Position = 0; CodedInputStream input = new CodedInputStream(ms); Assert.AreEqual(tag, input.ReadTag()); Assert.Throws <InvalidProtocolBufferException>(() => UnknownFieldSet.MergeFieldFrom(null, input)); }
public void MapWithOnlyKey_MessageValue() { // Hand-craft the stream to contain a single entry with just a key. var memoryStream = new MemoryStream(); var output = new CodedOutputStream(memoryStream); output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited); int key = 10; output.WriteLength(1 + CodedOutputStream.ComputeInt32Size(key)); output.WriteTag(1, WireFormat.WireType.Varint); output.WriteInt32(key); output.Flush(); var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); Assert.AreEqual(new ForeignMessage(), parsed.MapInt32ForeignMessage[key]); }
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); }
private static byte[] MakeMaliciousRecursionUnknownFieldsPayload(int recursionDepth) { // generate recursively nested groups that will be parsed as unknown fields int unknownFieldNumber = 14; // an unused field number MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); for (int i = 0; i < recursionDepth; i++) { output.WriteTag(WireFormat.MakeTag(unknownFieldNumber, WireFormat.WireType.StartGroup)); } for (int i = 0; i < recursionDepth; i++) { output.WriteTag(WireFormat.MakeTag(unknownFieldNumber, WireFormat.WireType.EndGroup)); } output.Flush(); return(ms.ToArray()); }
public void EndOfStreamReachedWhileSkippingGroup() { var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(1, WireFormat.WireType.StartGroup); output.WriteTag(2, WireFormat.WireType.StartGroup); output.WriteTag(2, WireFormat.WireType.EndGroup); output.Flush(); stream.Position = 0; // Now act like a generated client var input = new CodedInputStream(stream); input.ReadTag(); Assert.Throws <InvalidProtocolBufferException>(input.SkipLastField); }
public void MaximumFieldNumber() { MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); int fieldNumber = 0x1FFFFFFF; uint tag = WireFormat.MakeTag(fieldNumber, WireFormat.WireType.LengthDelimited); output.WriteRawVarint32(tag); output.WriteString("field 1"); output.Flush(); ms.Position = 0; CodedInputStream input = new CodedInputStream(ms); Assert.AreEqual(tag, input.ReadTag()); Assert.AreEqual(fieldNumber, WireFormat.GetTagFieldNumber(tag)); }
public static void WriteDelimitedTo(this IMessage message, Stream output) { Preconditions.CheckNotNull <IMessage>(message, Module.smethod_37 <string>(1018619835u)); while (true) { IL_97: uint arg_77_0 = 836366938u; while (true) { uint num; switch ((num = (arg_77_0 ^ 1222686458u)) % 5u) { case 0u: goto IL_97; case 1u: { Preconditions.CheckNotNull <Stream>(output, Module.smethod_36 <string>(1367025601u)); CodedOutputStream codedOutputStream = new CodedOutputStream(output); arg_77_0 = (num * 2302164566u ^ 2110158721u); continue; } case 2u: { CodedOutputStream codedOutputStream; codedOutputStream.WriteRawVarint32((uint)message.CalculateSize()); arg_77_0 = (num * 762481410u ^ 1229542845u); continue; } case 4u: { CodedOutputStream codedOutputStream; message.WriteTo(codedOutputStream); codedOutputStream.Flush(); arg_77_0 = (num * 187669686u ^ 2466296953u); continue; } } return; } } }
/// <summary> /// Parses the given bytes using WriteRawLittleEndian64() and checks /// that the result matches the given value. /// </summary> private static void AssertWriteLittleEndian64(byte[] data, ulong value) { MemoryStream rawOutput = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(rawOutput); output.WriteRawLittleEndian64(value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); // Try different block sizes. for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { rawOutput = new MemoryStream(); output = new CodedOutputStream(rawOutput, blockSize); output.WriteRawLittleEndian64(value); output.Flush(); Assert.AreEqual(data, rawOutput.ToArray()); } }
public void ReadMaliciouslyLargeBlob() { MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); output.WriteRawVarint32(tag); output.WriteRawVarint32(0x7FFFFFFF); output.WriteRawBytes(new byte[32]); // Pad with a few random bytes. output.Flush(); ms.Position = 0; CodedInputStream input = new CodedInputStream(ms); Assert.AreEqual(tag, input.ReadTag()); Assert.Throws <InvalidProtocolBufferException>(() => input.ReadBytes()); }
public void ExtraEndGroupSkipped() { var message = SampleMessages.CreateFullTestAllTypes(); var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(100, WireFormat.WireType.EndGroup); output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.WireType.Fixed32); output.WriteFixed32(123); output.Flush(); stream.Position = 0; var parsed = TestAllTypes.Parser.ParseFrom(stream); Assert.AreEqual(new TestAllTypes { SingleFixed32 = 123 }, parsed); }
public void ReadGroup_WrongEndGroupTag() { int groupFieldNumber = Proto2.TestAllTypes.OptionalGroupFieldNumber; // write Proto2.TestAllTypes with "optional_group" set, but use wrong EndGroup closing tag MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); output.WriteTag(WireFormat.MakeTag(groupFieldNumber, WireFormat.WireType.StartGroup)); output.WriteGroup(new Proto2.TestAllTypes.Types.OptionalGroup { A = 12345 }); // end group with different field number output.WriteTag(WireFormat.MakeTag(groupFieldNumber + 1, WireFormat.WireType.EndGroup)); output.Flush(); var payload = ms.ToArray(); Assert.Throws <InvalidProtocolBufferException>(() => Proto2.TestAllTypes.Parser.ParseFrom(payload)); }
public void ReadInvalidUtf8() { MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); output.WriteRawVarint32(tag); output.WriteRawVarint32(1); output.WriteRawBytes(new byte[] { 0x80 }); output.Flush(); ms.Position = 0; CodedInputStream input = new CodedInputStream(ms); Assert.AreEqual(tag, input.ReadTag()); string text = input.ReadString(); Assert.AreEqual('\ufffd', text[0]); }
private byte[] ObjectToByteBuffer(int descriptorId, object obj) { IMessage u = (IMessage)obj; int size = u.CalculateSize(); byte[] bytes = new byte[size]; CodedOutputStream cos = new CodedOutputStream(bytes); u.WriteTo(cos); cos.Flush(); WrappedMessage wm = new WrappedMessage(); wm.WrappedMessageBytes = ByteString.CopyFrom(bytes); wm.WrappedDescriptorId = descriptorId; byte[] msgBytes = new byte[wm.CalculateSize()]; CodedOutputStream msgCos = new CodedOutputStream(msgBytes); wm.WriteTo(msgCos); msgCos.Flush(); return msgBytes; }
public void CodedInputStream_LimitReachedRightAfterTag() { MemoryStream ms = new MemoryStream(); var cos = new CodedOutputStream(ms); cos.WriteTag(11, WireFormat.WireType.Varint); Assert.AreEqual(1, cos.Position); cos.WriteString("some extra padding"); // ensure is currentLimit distinct from the end of the buffer. cos.Flush(); var cis = new CodedInputStream(ms.ToArray()); cis.PushLimit(1); // make sure we reach the limit right after reading the tag. // we still must read the tag correctly, even though the tag is at the very end of our limited input // (which is a corner case and will most likely result in an error when trying to read value of the field // described by this tag, but it would be a logical error not to read the tag that's actually present). // See https://github.com/protocolbuffers/protobuf/pull/7289 cis.AssertNextTag(WireFormat.MakeTag(11, WireFormat.WireType.Varint)); }
byte[] _Serialize() { mSample.Id = 7761; mSample.Name = "Cho Myeong Geun"; mJsonText = GetjsonText(); var bufSize = mSample.CalculateSize(); using (var memStream = new MemoryStream(bufSize)) { using (var outStream = new PB.CodedOutputStream(memStream)) { mSample.WriteTo(outStream); outStream.Flush(); return(memStream.ToArray()); } } }
public void TestDefaultValue() { // WriteTagAndValue ignores default values var stream = new MemoryStream(); CodedOutputStream codedOutput; #if !NET35 codedOutput = new CodedOutputStream(stream); codec.WriteTagAndValue(codedOutput, codec.DefaultValue); codedOutput.Flush(); Assert.AreEqual(0, stream.Position); Assert.AreEqual(0, codec.CalculateSizeWithTag(codec.DefaultValue)); if (typeof(T).GetTypeInfo().IsValueType) { Assert.AreEqual(default(T), codec.DefaultValue); } #endif // The plain ValueWriter/ValueReader delegates don't. if (codec.DefaultValue != null) // This part isn't appropriate for message types. { codedOutput = new CodedOutputStream(stream); WriteContext.Initialize(codedOutput, out WriteContext ctx); try { // only write the value using the codec codec.ValueWriter(ref ctx, codec.DefaultValue); } finally { ctx.CopyStateTo(codedOutput); } codedOutput.Flush(); Assert.AreNotEqual(0, stream.Position); Assert.AreEqual(stream.Position, codec.ValueSizeCalculator(codec.DefaultValue)); stream.Position = 0; var codedInput = new CodedInputStream(stream); Assert.AreEqual(codec.DefaultValue, codec.Read(codedInput)); } }
public void MapWithOnlyValue() { // Hand-craft the stream to contain a single entry with just a value. var memoryStream = new MemoryStream(); var output = new CodedOutputStream(memoryStream); 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.WriteLength(2 + nestedMessage.CalculateSize()); output.WriteTag(2, WireFormat.WireType.LengthDelimited); output.WriteMessage(nestedMessage); output.Flush(); var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); Assert.AreEqual(nestedMessage, parsed.MapInt32ForeignMessage[0]); }
public void WriteStringsOfDifferentSizes_Ascii() { for (int i = 1; i <= 1024; i++) { var buffer = new byte[4096]; var output = new CodedOutputStream(buffer); var sb = new StringBuilder(); for (int j = 0; j < i; j++) { sb.Append((j % 10).ToString()); // incrementing numbers, repeating } var s = sb.ToString(); output.WriteString(s); output.Flush(); // Verify written content var input = new CodedInputStream(buffer); Assert.AreEqual(s, input.ReadString()); } }
public void RecursionLimitAppliedWhileSkippingGroup() { var stream = new MemoryStream(); var output = new CodedOutputStream(stream); for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) { output.WriteTag(1, WireFormat.WireType.StartGroup); } for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) { output.WriteTag(1, WireFormat.WireType.EndGroup); } output.Flush(); stream.Position = 0; // Now act like a generated client var input = new CodedInputStream(stream); Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.StartGroup), input.ReadTag()); Assert.Throws <InvalidProtocolBufferException>(input.SkipLastField); }
public void DiscardUnknownFields_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(); MessageParsingHelpers.AssertReadingMessage( TestAllTypes.Parser, stream.ToArray(), parsed => { // TODO(jieluo): Add test back when DiscardUnknownFields API is supported. // Assert.AreEqual(message, parsed); }); }
public void SetUp () { // Create a request object and get the binary representation of it expectedRequest = new Request ("TestService", "ProcedureNoArgsNoReturn"); using (var stream = new MemoryStream ()) { var codedStream = new CodedOutputStream (stream, true); codedStream.WriteInt32 (expectedRequest.ToProtobufMessage ().CalculateSize ()); expectedRequest.ToProtobufMessage ().WriteTo (codedStream); codedStream.Flush (); requestBytes = stream.ToArray (); } // Create a response object and get the binary representation of it expectedResponse = new Response (); expectedResponse.Error = "SomeErrorMessage"; expectedResponse.Time = 42; using (var stream = new MemoryStream ()) { var codedStream = new CodedOutputStream (stream, true); codedStream.WriteInt32 (expectedResponse.ToProtobufMessage ().CalculateSize ()); expectedResponse.ToProtobufMessage ().WriteTo (codedStream); codedStream.Flush (); responseBytes = stream.ToArray (); } }
public void SetUp() { // Create a request object and get the binary representation of it expectedRequest = new Request (); expectedRequest.Service = "SomeServiceName"; expectedRequest.Procedure = "SomeMethodName"; using (var stream = new MemoryStream ()) { var codedStream = new CodedOutputStream (stream); codedStream.WriteInt32 (expectedRequest.CalculateSize ()); expectedRequest.WriteTo (codedStream); codedStream.Flush (); requestBytes = stream.ToArray (); } // Create a response object and get the binary representation of it expectedResponse = new Response (); expectedResponse.Error = "SomeErrorMessage"; expectedResponse.Time = 42; using (var stream = new MemoryStream ()) { var codedStream = new CodedOutputStream (stream); codedStream.WriteInt32 (expectedResponse.CalculateSize ()); expectedResponse.WriteTo (codedStream); codedStream.Flush (); responseBytes = stream.ToArray (); } }
public void ReadInvalidUtf8() { MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); output.WriteRawVarint32(tag); output.WriteRawVarint32(1); output.WriteRawBytes(new byte[] {0x80}); output.Flush(); ms.Position = 0; CodedInputStream input = new CodedInputStream(ms); Assert.AreEqual(tag, input.ReadTag()); string text = input.ReadString(); Assert.AreEqual('\ufffd', text[0]); }
public void MapFieldOrderIsIrrelevant() { var memoryStream = new MemoryStream(); var output = new CodedOutputStream(memoryStream); output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); var key = 10; var value = 20; // Each field can be represented in a single byte, with a single byte tag. // Total message size: 4 bytes. output.WriteLength(4); output.WriteTag(2, WireFormat.WireType.Varint); output.WriteInt32(value); output.WriteTag(1, WireFormat.WireType.Varint); output.WriteInt32(key); output.Flush(); var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); Assert.AreEqual(value, parsed.MapInt32Int32[key]); }
/// <summary> /// Writes the length and then data of the given message to a stream. /// </summary> /// <param name="message">The message to write.</param> /// <param name="output">The output stream to write to.</param> public static void WriteDelimitedTo(this IMessage message, Stream output) { ProtoPreconditions.CheckNotNull(message, "message"); ProtoPreconditions.CheckNotNull(output, "output"); CodedOutputStream codedOutput = new CodedOutputStream(output); codedOutput.WriteRawVarint32((uint)message.CalculateSize()); message.WriteTo(codedOutput); codedOutput.Flush(); }
public void ExtraEndGroupThrows() { var message = SampleMessages.CreateFullTestAllTypes(); var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.WireType.Fixed32); output.WriteFixed32(123); output.WriteTag(100, WireFormat.WireType.EndGroup); output.Flush(); stream.Position = 0; Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseFrom(stream)); }
public void EndOfStreamReachedWhileSkippingGroup() { var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(1, WireFormat.WireType.StartGroup); output.WriteTag(2, WireFormat.WireType.StartGroup); output.WriteTag(2, WireFormat.WireType.EndGroup); output.Flush(); stream.Position = 0; // Now act like a generated client var input = new CodedInputStream(stream); input.ReadTag(); Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField); }
public void TestSlowPathAvoidance() { using (var ms = new MemoryStream()) { CodedOutputStream output = new CodedOutputStream(ms); output.WriteTag(1, WireFormat.WireType.LengthDelimited); output.WriteBytes(ByteString.CopyFrom(new byte[100])); output.WriteTag(2, WireFormat.WireType.LengthDelimited); output.WriteBytes(ByteString.CopyFrom(new byte[100])); output.Flush(); ms.Position = 0; CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2], 0, 0); uint tag = input.ReadTag(); Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag)); Assert.AreEqual(100, input.ReadBytes().Length); tag = input.ReadTag(); Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag)); Assert.AreEqual(100, input.ReadBytes().Length); } }
private byte[] IntToByteBuffer(int i) { int t = CodedOutputStream.ComputeTagSize(5); int s = CodedOutputStream.ComputeInt32Size(i); s += t; byte[] bytes = new byte[s]; CodedOutputStream cos = new CodedOutputStream(bytes); cos.WriteTag((5 << 3) + 0); cos.WriteInt32(i); cos.Flush(); return bytes; }
static ByteString EncodeObject (object value, Type type, MemoryStream buffer, CodedOutputStream stream) { buffer.SetLength (0); if (value != null && !type.IsInstanceOfType (value)) throw new ArgumentException ("Value of type " + value.GetType () + " cannot be encoded to type " + type); if (value == null && !type.IsSubclassOf (typeof(RemoteObject)) && !IsACollectionType (type)) throw new ArgumentException ("null cannot be encoded to type " + type); if (value == null) stream.WriteUInt64 (0); else if (value is Enum) stream.WriteInt32 ((int)value); else { switch (Type.GetTypeCode (type)) { case TypeCode.Int32: stream.WriteInt32 ((int)value); break; case TypeCode.Int64: stream.WriteInt64 ((long)value); break; case TypeCode.UInt32: stream.WriteUInt32 ((uint)value); break; case TypeCode.UInt64: stream.WriteUInt64 ((ulong)value); break; case TypeCode.Single: stream.WriteFloat ((float)value); break; case TypeCode.Double: stream.WriteDouble ((double)value); break; case TypeCode.Boolean: stream.WriteBool ((bool)value); break; case TypeCode.String: stream.WriteString ((string)value); break; default: if (type.Equals (typeof(byte[]))) stream.WriteBytes (ByteString.CopyFrom ((byte[])value)); else if (IsAClassType (type)) stream.WriteUInt64 (((RemoteObject)value).id); else if (IsAMessageType (type)) ((IMessage)value).WriteTo (buffer); else if (IsAListType (type)) WriteList (value, type, buffer); else if (IsADictionaryType (type)) WriteDictionary (value, type, buffer); else if (IsASetType (type)) WriteSet (value, type, buffer); else if (IsATupleType (type)) WriteTuple (value, type, buffer); else throw new ArgumentException (type + " is not a serializable type"); break; } } stream.Flush (); return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length); }
/// <summary> /// Writes the given message data to the given stream in protobuf encoding. /// </summary> /// <param name="message">The message to write to the stream.</param> /// <param name="output">The stream to write to.</param> public static void WriteTo(this IMessage message, Stream output) { ProtoPreconditions.CheckNotNull(message, "message"); ProtoPreconditions.CheckNotNull(output, "output"); CodedOutputStream codedOutput = new CodedOutputStream(output); message.WriteTo(codedOutput); codedOutput.Flush(); }
/// <summary> /// Encode a value of the given type. /// Should not be called directly. This interface is used by service client stubs. /// </summary> public static ByteString Encode(object value, Type type) { var stream = new MemoryStream (); var encoder = new CodedOutputStream (stream); if (value != null && !type.IsAssignableFrom (value.GetType ())) //TODO: nulls? throw new ArgumentException ("Value of type " + value.GetType () + " cannot be encoded to type " + type); if (type == typeof(Double)) encoder.WriteDouble ((Double)value); else if (type == typeof(Single)) encoder.WriteFloat ((Single)value); else if (type == typeof(Int32)) encoder.WriteInt32 ((Int32)value); else if (type == typeof(Int64)) encoder.WriteInt64 ((Int64)value); else if (type == typeof(UInt32)) encoder.WriteUInt32 ((UInt32)value); else if (type == typeof(UInt64)) encoder.WriteUInt64 ((UInt64)value); else if (type == typeof(Boolean)) encoder.WriteBool ((Boolean)value); else if (type == typeof(String)) encoder.WriteString ((String)value); else if (type == typeof(byte[])) encoder.WriteBytes (ByteString.CopyFrom ((byte[])value)); else if (value != null && value is Enum) encoder.WriteInt32 ((int)value); else if (type.IsSubclassOf (typeof(RemoteObject))) { if (value == null) encoder.WriteUInt64 (0); else encoder.WriteUInt64 (((RemoteObject)value)._ID); } else if (value != null && value is IMessage) { ((IMessage)value).WriteTo (stream); return ByteString.CopyFrom (stream.ToArray ()); } else if (value != null && value is IList) return EncodeList (value, type); else if (value != null && value is IDictionary) return EncodeDictionary (value, type); else if (value != null && value.GetType ().IsGenericType && value.GetType ().GetGenericTypeDefinition () == typeof(HashSet<>)) return EncodeSet (value, type); //TODO: ugly checking for set types else if (value != null && value.GetType ().IsGenericType && (value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,,,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,,,,>))) return EncodeTuple (value, type); //TODO: ugly checking for tuple types else throw new ArgumentException (type + " is not a serializable type"); encoder.Flush (); return ByteString.CopyFrom (stream.ToArray ()); }
public void TestCodedInputOutputPosition() { byte[] content = new byte[110]; for (int i = 0; i < content.Length; i++) content[i] = (byte)i; byte[] child = new byte[120]; { MemoryStream ms = new MemoryStream(child); CodedOutputStream cout = new CodedOutputStream(ms, 20); // Field 11: numeric value: 500 cout.WriteTag(11, WireFormat.WireType.Varint); Assert.AreEqual(1, cout.Position); cout.WriteInt32(500); Assert.AreEqual(3, cout.Position); //Field 12: length delimited 120 bytes cout.WriteTag(12, WireFormat.WireType.LengthDelimited); Assert.AreEqual(4, cout.Position); cout.WriteBytes(ByteString.CopyFrom(content)); Assert.AreEqual(115, cout.Position); // Field 13: fixed numeric value: 501 cout.WriteTag(13, WireFormat.WireType.Fixed32); Assert.AreEqual(116, cout.Position); cout.WriteSFixed32(501); Assert.AreEqual(120, cout.Position); cout.Flush(); } byte[] bytes = new byte[130]; { CodedOutputStream cout = new CodedOutputStream(bytes); // Field 1: numeric value: 500 cout.WriteTag(1, WireFormat.WireType.Varint); Assert.AreEqual(1, cout.Position); cout.WriteInt32(500); Assert.AreEqual(3, cout.Position); //Field 2: length delimited 120 bytes cout.WriteTag(2, WireFormat.WireType.LengthDelimited); Assert.AreEqual(4, cout.Position); cout.WriteBytes(ByteString.CopyFrom(child)); Assert.AreEqual(125, cout.Position); // Field 3: fixed numeric value: 500 cout.WriteTag(3, WireFormat.WireType.Fixed32); Assert.AreEqual(126, cout.Position); cout.WriteSFixed32(501); Assert.AreEqual(130, cout.Position); cout.Flush(); } // Now test Input stream: { CodedInputStream cin = new CodedInputStream(new MemoryStream(bytes), new byte[50], 0, 0); Assert.AreEqual(0, cin.Position); // Field 1: uint tag = cin.ReadTag(); Assert.AreEqual(1, tag >> 3); Assert.AreEqual(1, cin.Position); Assert.AreEqual(500, cin.ReadInt32()); Assert.AreEqual(3, cin.Position); //Field 2: tag = cin.ReadTag(); Assert.AreEqual(2, tag >> 3); Assert.AreEqual(4, cin.Position); int childlen = cin.ReadLength(); Assert.AreEqual(120, childlen); Assert.AreEqual(5, cin.Position); int oldlimit = cin.PushLimit((int)childlen); Assert.AreEqual(5, cin.Position); // Now we are reading child message { // Field 11: numeric value: 500 tag = cin.ReadTag(); Assert.AreEqual(11, tag >> 3); Assert.AreEqual(6, cin.Position); Assert.AreEqual(500, cin.ReadInt32()); Assert.AreEqual(8, cin.Position); //Field 12: length delimited 120 bytes tag = cin.ReadTag(); Assert.AreEqual(12, tag >> 3); Assert.AreEqual(9, cin.Position); ByteString bstr = cin.ReadBytes(); Assert.AreEqual(110, bstr.Length); Assert.AreEqual((byte) 109, bstr[109]); Assert.AreEqual(120, cin.Position); // Field 13: fixed numeric value: 501 tag = cin.ReadTag(); Assert.AreEqual(13, tag >> 3); // ROK - Previously broken here, this returned 126 failing to account for bufferSizeAfterLimit Assert.AreEqual(121, cin.Position); Assert.AreEqual(501, cin.ReadSFixed32()); Assert.AreEqual(125, cin.Position); Assert.IsTrue(cin.IsAtEnd); } cin.PopLimit(oldlimit); Assert.AreEqual(125, cin.Position); // Field 3: fixed numeric value: 501 tag = cin.ReadTag(); Assert.AreEqual(3, tag >> 3); Assert.AreEqual(126, cin.Position); Assert.AreEqual(501, cin.ReadSFixed32()); Assert.AreEqual(130, cin.Position); Assert.IsTrue(cin.IsAtEnd); } }
public void DuplicateKeys_LastEntryWins() { var memoryStream = new MemoryStream(); var output = new CodedOutputStream(memoryStream); var key = 10; var value1 = 20; var value2 = 30; // First entry output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); output.WriteLength(4); output.WriteTag(1, WireFormat.WireType.Varint); output.WriteInt32(key); output.WriteTag(2, WireFormat.WireType.Varint); output.WriteInt32(value1); // Second entry - same key, different value output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); output.WriteLength(4); output.WriteTag(1, WireFormat.WireType.Varint); output.WriteInt32(key); output.WriteTag(2, WireFormat.WireType.Varint); output.WriteInt32(value2); output.Flush(); var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); Assert.AreEqual(value2, parsed.MapInt32Int32[key]); }
public void ReadMaliciouslyLargeBlob() { MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); output.WriteRawVarint32(tag); output.WriteRawVarint32(0x7FFFFFFF); output.WriteRawBytes(new byte[32]); // Pad with a few random bytes. output.Flush(); ms.Position = 0; CodedInputStream input = new CodedInputStream(ms); Assert.AreEqual(tag, input.ReadTag()); Assert.Throws<InvalidProtocolBufferException>(() => input.ReadBytes()); }
public void SkipGroup_WrongEndGroupTag() { // Create an output stream with: // Field 1: string "field 1" // Start group 2 // Field 3: fixed int32 // End group 4 (should give an error) var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(1, WireFormat.WireType.LengthDelimited); output.WriteString("field 1"); // The outer group... output.WriteTag(2, WireFormat.WireType.StartGroup); output.WriteTag(3, WireFormat.WireType.Fixed32); output.WriteFixed32(100); output.WriteTag(4, WireFormat.WireType.EndGroup); output.Flush(); stream.Position = 0; // Now act like a generated client var input = new CodedInputStream(stream); Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag()); Assert.AreEqual("field 1", input.ReadString()); Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag()); Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField); }
public void MapIgnoresExtraFieldsWithinEntryMessages() { // Hand-craft the stream to contain a single entry with three fields var memoryStream = new MemoryStream(); var output = new CodedOutputStream(memoryStream); output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); var key = 10; // Field 1 var value = 20; // Field 2 var extra = 30; // Field 3 // Each field can be represented in a single byte, with a single byte tag. // Total message size: 6 bytes. output.WriteLength(6); output.WriteTag(1, WireFormat.WireType.Varint); output.WriteInt32(key); output.WriteTag(2, WireFormat.WireType.Varint); output.WriteInt32(value); output.WriteTag(3, WireFormat.WireType.Varint); output.WriteInt32(extra); output.Flush(); var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); Assert.AreEqual(value, parsed.MapInt32Int32[key]); }
public void RecursionLimitAppliedWhileSkippingGroup() { var stream = new MemoryStream(); var output = new CodedOutputStream(stream); for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) { output.WriteTag(1, WireFormat.WireType.StartGroup); } for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) { output.WriteTag(1, WireFormat.WireType.EndGroup); } output.Flush(); stream.Position = 0; // Now act like a generated client var input = new CodedInputStream(stream); Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.StartGroup), input.ReadTag()); Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField); }
public void MapNonContiguousEntries() { var memoryStream = new MemoryStream(); var output = new CodedOutputStream(memoryStream); // Message structure: // Entry for MapInt32Int32 // Entry for MapStringString // Entry for MapInt32Int32 // First entry var key1 = 10; var value1 = 20; output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); output.WriteLength(4); output.WriteTag(1, WireFormat.WireType.Varint); output.WriteInt32(key1); output.WriteTag(2, WireFormat.WireType.Varint); output.WriteInt32(value1); // Second entry var key2 = "a"; var value2 = "b"; output.WriteTag(TestMap.MapStringStringFieldNumber, WireFormat.WireType.LengthDelimited); 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); output.WriteString(value2); // Third entry var key3 = 15; var value3 = 25; output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); output.WriteLength(4); output.WriteTag(1, WireFormat.WireType.Varint); output.WriteInt32(key3); output.WriteTag(2, WireFormat.WireType.Varint); output.WriteInt32(value3); output.Flush(); var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); var expected = new TestMap { MapInt32Int32 = { { key1, value1 }, { key3, value3 } }, MapStringString = { { key2, value2 } } }; Assert.AreEqual(expected, parsed); }
public void RogueEndGroupTag() { // If we have an end-group tag without a leading start-group tag, generated // code will just call SkipLastField... so that should fail. var stream = new MemoryStream(); var output = new CodedOutputStream(stream); output.WriteTag(1, WireFormat.WireType.EndGroup); output.Flush(); stream.Position = 0; var input = new CodedInputStream(stream); Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.EndGroup), input.ReadTag()); Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField); }