예제 #1
0
        public void SkipWholeMessage()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            byte[] rawBytes = message.ToByteArray();

            // Create two parallel inputs.  Parse one as unknown fields while using
            // skipField() to skip each field on the other.  Expect the same tags.
            CodedInputStream input1 = CodedInputStream.CreateInstance(rawBytes);
            CodedInputStream input2 = CodedInputStream.CreateInstance(rawBytes);

            UnknownFieldSet.Builder unknownFields = UnknownFieldSet.CreateBuilder();

            uint   tag;
            string name;

            while (input1.ReadTag(out tag, out name))
            {
                uint tag2;
                Assert.IsTrue(input2.ReadTag(out tag2, out name));
                Assert.AreEqual(tag, tag2);

                unknownFields.MergeFieldFrom(tag, input1);
                input2.SkipField();
            }
        }
        public void RoundTrip_Groups()
        {
            var message = new TestAllTypes
            {
                OptionalGroup = new TestAllTypes.Types.OptionalGroup
                {
                    A = 10
                },
                RepeatedGroup =
                {
                    new TestAllTypes.Types.RepeatedGroup {
                        A = 10
                    },
                    new TestAllTypes.Types.RepeatedGroup {
                        A = 20
                    },
                    new TestAllTypes.Types.RepeatedGroup {
                        A = 30
                    }
                }
            };

            byte[]       bytes  = message.ToByteArray();
            TestAllTypes parsed = Proto2.TestAllTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual(message, parsed);
        }
        /// <returns>A serialized big message</returns>
        private static byte[] GenerateBigSerializedMessage()
        {
            byte[]       value   = new byte[16 * 1024 * 1024];
            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();

            message.SingleBytes = ByteString.CopyFrom(value);
            return(message.ToByteArray());
        }
예제 #4
0
        public void RoundTrip_Empty()
        {
            var message = new TestAllTypes();

            // Without setting any values, there's nothing to write.
            byte[] bytes = message.ToByteArray();
            Assert.AreEqual(0, bytes.Length);

            MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message);
        }
예제 #5
0
        public void RoundTrip_Empty()
        {
            var message = new TestAllTypes();

            // Without setting any values, there's nothing to write.
            byte[] bytes = message.ToByteArray();
            Assert.AreEqual(0, bytes.Length);
            TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual(message, parsed);
        }
예제 #6
0
        public void TestRoundTripRepeatedTypes()
        {
            TestAllTypes msg     = AddRepeatedTypes(new TestAllTypes.Builder(), 5).Build();
            object       content = SerializeMessage <TestAllTypes, TestAllTypes.Builder>(msg);

            TestAllTypes copy = DeserializeMessage <TestAllTypes, TestAllTypes.Builder>(content, TestAllTypes.CreateBuilder(), ExtensionRegistry.Empty).Build();

            Assert.AreEqual(msg, copy);
            AssertOutputEquals(content, SerializeMessage <TestAllTypes, TestAllTypes.Builder>(copy));
            Assert.AreEqual(Convert.ToBase64String(msg.ToByteArray()), Convert.ToBase64String(copy.ToByteArray()));
        }
예제 #7
0
        public void OneofSerialization_DefaultValue()
        {
            var message = new TestAllTypes();

            message.OneofString = "this would take a bit of space";
            message.OneofUint32 = 0; // This is the default value for UInt32; normally wouldn't be serialized
            var bytes = message.ToByteArray();

            Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - it's still serialized

            var message2 = TestAllTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual(message, message2);
            Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
        }
예제 #8
0
        public void OneofSerialization_NonDefaultValue()
        {
            var message = new TestAllTypes();

            message.OneofString = "this would take a bit of space";
            message.OneofUint32 = 10;
            var bytes = message.ToByteArray();

            Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - no string!

            var message2 = TestAllTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual(message, message2);
            Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
        }
예제 #9
0
        public void OneofSerialization_NonDefaultValue()
        {
            var message = new TestAllTypes();

            message.OneofString = "this would take a bit of space";
            message.OneofUint32 = 10;
            var bytes = message.ToByteArray();

            Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - no string!

            MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message, parsedMessage =>
            {
                Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, parsedMessage.OneofFieldCase);
            });
        }
예제 #10
0
        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());
            }
        }
예제 #11
0
        public void RoundTrip_RepeatedValues()
        {
            var message = new TestAllTypes
            {
                RepeatedBool           = { true, false },
                RepeatedBytes          = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6) },
                RepeatedDouble         = { -12.25, 23.5 },
                RepeatedFixed32        = { uint.MaxValue, 23 },
                RepeatedFixed64        = { ulong.MaxValue, 1234567890123 },
                RepeatedFloat          = { 100f, 12.25f },
                RepeatedForeignEnum    = { ForeignEnum.FOREIGN_FOO, ForeignEnum.FOREIGN_BAR },
                RepeatedForeignMessage = { new ForeignMessage(), new ForeignMessage {
                                               C = 10
                                           } },
                RepeatedImportEnum    = { ImportEnum.IMPORT_BAZ, ImportEnum.IMPORT_ENUM_UNSPECIFIED },
                RepeatedImportMessage = { new ImportMessage {
                                              D = 20
                                          }, new ImportMessage{
                                              D = 25
                                          } },
                RepeatedInt32         = { 100, 200 },
                RepeatedInt64         = { 3210987654321, long.MaxValue },
                RepeatedNestedEnum    = { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.NEG },
                RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage {
                                              Bb = 35
                                          }, new TestAllTypes.Types.NestedMessage{
                                              Bb = 10
                                          } },
                RepeatedPublicImportMessage = { new PublicImportMessage {
                                                    E = 54
                                                }, new PublicImportMessage{
                                                    E = -1
                                                } },
                RepeatedSfixed32 = { -123, 123 },
                RepeatedSfixed64 = { -12345678901234, 12345678901234 },
                RepeatedSint32   = { -456, 100 },
                RepeatedSint64   = { -12345678901235, 123 },
                RepeatedString   = { "foo", "bar" },
                RepeatedUint32   = { uint.MaxValue, uint.MinValue },
                RepeatedUint64   = { ulong.MaxValue, uint.MinValue }
            };

            byte[]       bytes  = message.ToByteArray();
            TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual(message, parsed);
        }
        public void ReadWholeMessage_VaryingBlockSizes()
        {
            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();

            byte[] rawBytes = message.ToByteArray();
            Assert.AreEqual(rawBytes.Length, message.CalculateSize());
            TestAllTypes message2 = TestAllTypes.Parser.ParseFrom(rawBytes);

            Assert.AreEqual(message, message2);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                message2 = TestAllTypes.Parser.ParseFrom(new SmallBlockInputStream(rawBytes, blockSize));
                Assert.AreEqual(message, message2);
            }
        }
예제 #13
0
        public void ReadWholeMessage_VaryingBlockSizes_FromSequence()
        {
            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();

            byte[] rawBytes = message.ToByteArray();
            Assert.AreEqual(rawBytes.Length, message.CalculateSize());
            TestAllTypes message2 = TestAllTypes.Parser.ParseFrom(rawBytes);

            Assert.AreEqual(message, message2);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                message2 = TestAllTypes.Parser.ParseFrom(ReadOnlySequenceFactory.CreateWithContent(rawBytes, blockSize));
                Assert.AreEqual(message, message2);
            }
        }
예제 #14
0
        public void RoundTrip_RepeatedValues()
        {
            var message = new TestAllTypes
            {
                RepeatedBool           = { true, false },
                RepeatedBytes          = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6) },
                RepeatedDouble         = { -12.25, 23.5 },
                RepeatedFixed32        = { uint.MaxValue, 23 },
                RepeatedFixed64        = { ulong.MaxValue, 1234567890123 },
                RepeatedFloat          = { 100f, 12.25f },
                RepeatedForeignEnum    = { ForeignEnum.ForeignFoo, ForeignEnum.ForeignBar },
                RepeatedForeignMessage = { new ForeignMessage(), new ForeignMessage {
                                               C = 10
                                           } },
                RepeatedImportEnum    = { ImportEnum.ImportBaz, ImportEnum.Unspecified },
                RepeatedImportMessage = { new ImportMessage {
                                              D = 20
                                          }, new ImportMessage{
                                              D = 25
                                          } },
                RepeatedInt32         = { 100, 200 },
                RepeatedInt64         = { 3210987654321, long.MaxValue },
                RepeatedNestedEnum    = { TestAllTypes.Types.NestedEnum.Foo, TestAllTypes.Types.NestedEnum.Neg },
                RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage {
                                              Bb = 35
                                          }, new TestAllTypes.Types.NestedMessage{
                                              Bb = 10
                                          } },
                RepeatedPublicImportMessage = { new PublicImportMessage {
                                                    E = 54
                                                }, new PublicImportMessage{
                                                    E = -1
                                                } },
                RepeatedSfixed32 = { -123, 123 },
                RepeatedSfixed64 = { -12345678901234, 12345678901234 },
                RepeatedSint32   = { -456, 100 },
                RepeatedSint64   = { -12345678901235, 123 },
                RepeatedString   = { "foo", "bar" },
                RepeatedUint32   = { uint.MaxValue, uint.MinValue },
                RepeatedUint64   = { ulong.MaxValue, uint.MinValue }
            };

            byte[] bytes = message.ToByteArray();

            MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message);
        }
예제 #15
0
        public void ReadWholeMessage()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            byte[] rawBytes = message.ToByteArray();
            Assert.AreEqual(rawBytes.Length, message.SerializedSize);
            TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);

            TestUtil.AssertAllFieldsSet(message2);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                message2 = TestAllTypes.ParseFrom(new SmallBlockInputStream(rawBytes, blockSize));
                TestUtil.AssertAllFieldsSet(message2);
            }
        }
        public void OneofSerialization_DefaultValue()
        {
            var message = new TestAllTypes();

            message.OneofString = "this would take a bit of space";
            message.OneofUint32 = 0; // This is the default value for UInt32; normally wouldn't be serialized
            var bytes = message.ToByteArray();

            Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - it's still serialized

            MessageParsingHelpers.AssertWritingMessage(message);

            MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message, parsedMessage =>
            {
                Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, parsedMessage.OneofFieldCase);
            });
        }
예제 #17
0
        public void WriteWholeMessage()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            byte[] rawBytes = message.ToByteArray();
            TestUtil.AssertEqualBytes(TestUtil.GoldenMessage.ToByteArray(), rawBytes);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                MemoryStream      rawOutput = new MemoryStream();
                CodedOutputStream output    =
                    CodedOutputStream.CreateInstance(rawOutput, blockSize);
                message.WriteTo(output);
                output.Flush();
                TestUtil.AssertEqualBytes(rawBytes, rawOutput.ToArray());
            }
        }
예제 #18
0
        public void RoundTrip_SingleValues()
        {
            var message = new TestAllTypes
            {
                SingleBool           = true,
                SingleBytes          = ByteString.CopyFrom(1, 2, 3, 4),
                SingleDouble         = 23.5,
                SingleFixed32        = 23,
                SingleFixed64        = 1234567890123,
                SingleFloat          = 12.25f,
                SingleForeignEnum    = ForeignEnum.FOREIGN_BAR,
                SingleForeignMessage = new ForeignMessage {
                    C = 10
                },
                SingleImportEnum    = ImportEnum.IMPORT_BAZ,
                SingleImportMessage = new ImportMessage {
                    D = 20
                },
                SingleInt32         = 100,
                SingleInt64         = 3210987654321,
                SingleNestedEnum    = TestAllTypes.Types.NestedEnum.FOO,
                SingleNestedMessage = new TestAllTypes.Types.NestedMessage {
                    Bb = 35
                },
                SinglePublicImportMessage = new PublicImportMessage {
                    E = 54
                },
                SingleSfixed32 = -123,
                SingleSfixed64 = -12345678901234,
                SingleSint32   = -456,
                SingleSint64   = -12345678901235,
                SingleString   = "test",
                SingleUint32   = uint.MaxValue,
                SingleUint64   = ulong.MaxValue
            };

            byte[]       bytes  = message.ToByteArray();
            TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual(message, parsed);
        }
예제 #19
0
        public void RoundTrip_SingleValues()
        {
            var message = new TestAllTypes
            {
                SingleBool           = true,
                SingleBytes          = ByteString.CopyFrom(1, 2, 3, 4),
                SingleDouble         = 23.5,
                SingleFixed32        = 23,
                SingleFixed64        = 1234567890123,
                SingleFloat          = 12.25f,
                SingleForeignEnum    = ForeignEnum.ForeignBar,
                SingleForeignMessage = new ForeignMessage {
                    C = 10
                },
                SingleImportEnum    = ImportEnum.ImportBaz,
                SingleImportMessage = new ImportMessage {
                    D = 20
                },
                SingleInt32         = 100,
                SingleInt64         = 3210987654321,
                SingleNestedEnum    = TestAllTypes.Types.NestedEnum.Foo,
                SingleNestedMessage = new TestAllTypes.Types.NestedMessage {
                    Bb = 35
                },
                SinglePublicImportMessage = new PublicImportMessage {
                    E = 54
                },
                SingleSfixed32 = -123,
                SingleSfixed64 = -12345678901234,
                SingleSint32   = -456,
                SingleSint64   = -12345678901235,
                SingleString   = "test",
                SingleUint32   = uint.MaxValue,
                SingleUint64   = ulong.MaxValue
            };

            byte[] bytes = message.ToByteArray();

            MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message);
        }
예제 #20
0
        public void OneofSerialization_DefaultValue()
        {
            var message = new TestAllTypes();
            message.OneofString = "this would take a bit of space";
            message.OneofUint32 = 0; // This is the default value for UInt32; normally wouldn't be serialized
            var bytes = message.ToByteArray();
            Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - it's still serialized

            var message2 = TestAllTypes.Parser.ParseFrom(bytes);
            Assert.AreEqual(message, message2);
            Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
        }
예제 #21
0
        public void OneofSerialization_NonDefaultValue()
        {
            var message = new TestAllTypes();
            message.OneofString = "this would take a bit of space";
            message.OneofUint32 = 10;
            var bytes = message.ToByteArray();
            Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - no string!

            var message2 = TestAllTypes.Parser.ParseFrom(bytes);
            Assert.AreEqual(message, message2);
            Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
        }
예제 #22
0
        public void RoundTrip_RepeatedValues()
        {
            var message = new TestAllTypes
            {
                RepeatedBool = { true, false },
                RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6) },
                RepeatedDouble = { -12.25, 23.5 },
                RepeatedFixed32 = { uint.MaxValue, 23 },
                RepeatedFixed64 = { ulong.MaxValue, 1234567890123 },
                RepeatedFloat = { 100f, 12.25f },
                RepeatedForeignEnum = { ForeignEnum.FOREIGN_FOO, ForeignEnum.FOREIGN_BAR },
                RepeatedForeignMessage = { new ForeignMessage(), new ForeignMessage { C = 10 } },
                RepeatedImportEnum = { ImportEnum.IMPORT_BAZ, ImportEnum.IMPORT_ENUM_UNSPECIFIED },
                RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } },
                RepeatedInt32 = { 100, 200 },
                RepeatedInt64 = { 3210987654321, long.MaxValue },
                RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.NEG },
                RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } },
                RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } },
                RepeatedSfixed32 = { -123, 123 },
                RepeatedSfixed64 = { -12345678901234, 12345678901234 },
                RepeatedSint32 = { -456, 100 },
                RepeatedSint64 = { -12345678901235, 123 },
                RepeatedString = { "foo", "bar" },
                RepeatedUint32 = { uint.MaxValue, uint.MinValue },
                RepeatedUint64 = { ulong.MaxValue, uint.MinValue }
            };

            byte[] bytes = message.ToByteArray();
            TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);
            Assert.AreEqual(message, parsed);
        }
예제 #23
0
        public void RoundTrip_SingleValues()
        {
            var message = new TestAllTypes
            {
                SingleBool = true,
                SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
                SingleDouble = 23.5,
                SingleFixed32 = 23,
                SingleFixed64 = 1234567890123,
                SingleFloat = 12.25f,
                SingleForeignEnum = ForeignEnum.FOREIGN_BAR,
                SingleForeignMessage = new ForeignMessage { C = 10 },
                SingleImportEnum = ImportEnum.IMPORT_BAZ,
                SingleImportMessage = new ImportMessage { D = 20 },
                SingleInt32 = 100,
                SingleInt64 = 3210987654321,
                SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO,
                SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
                SinglePublicImportMessage = new PublicImportMessage { E = 54 },
                SingleSfixed32 = -123,
                SingleSfixed64 = -12345678901234,
                SingleSint32 = -456,
                SingleSint64 = -12345678901235,
                SingleString = "test",
                SingleUint32 = uint.MaxValue,
                SingleUint64 = ulong.MaxValue
            };

            byte[] bytes = message.ToByteArray();
            TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);
            Assert.AreEqual(message, parsed);
        }
예제 #24
0
 public void RoundTrip_Empty()
 {
     var message = new TestAllTypes();
     // Without setting any values, there's nothing to write.
     byte[] bytes = message.ToByteArray();
     Assert.AreEqual(0, bytes.Length);
     TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);
     Assert.AreEqual(message, parsed);
 }