public void ReadHugeBlob() { // Allocate and initialize a 1MB blob. byte[] blob = new byte[1 << 20]; for (int i = 0; i < blob.Length; i++) { blob[i] = (byte)i; } // Make a message containing it. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); TestUtil.SetAllFields(builder); builder.SetOptionalBytes(ByteString.CopyFrom(blob)); TestAllTypes message = builder.Build(); // Serialize and parse it. Make sure to parse from an InputStream, not // directly from a ByteString, so that CodedInputStream uses buffered // reading. TestAllTypes message2 = TestAllTypes.ParseFrom(message.ToByteString().CreateCodedInput()); Assert.AreEqual(message.OptionalBytes, message2.OptionalBytes); // Make sure all the other fields were parsed correctly. TestAllTypes message3 = TestAllTypes.CreateBuilder(message2) .SetOptionalBytes(TestUtil.GetAllSet().OptionalBytes) .Build(); TestUtil.AssertAllFieldsSet(message3); }
public void Serialization() { IMessage abstractMessage = new AbstractMessageWrapper(TestUtil.GetAllSet()); TestUtil.AssertAllFieldsSet(TestAllTypes.ParseFrom(abstractMessage.ToByteString())); Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), abstractMessage.ToByteString()); }
public void SizeLimit() { // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't // apply to the latter case. MemoryStream ms = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray()); CodedInputStream input = CodedInputStream.CreateInstance(ms); input.SetSizeLimit(16); Assert.Throws <InvalidProtocolBufferException>(() => TestAllTypes.ParseFrom(input)); }
public void Serialization() { TestAllTypes message = TestUtil.GetAllSet(); ByteString rawBytes = message.ToByteString(); Assert.AreEqual(rawBytes.Length, message.SerializedSize); TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes); TestUtil.AssertAllFieldsSet(message2); }
public void WrongTypeTreatedAsUnknown() { // Test that fields of the wrong wire type are treated like unknown fields // when parsing. ByteString bizarroData = GetBizarroData(); TestAllTypes allTypesMessage = TestAllTypes.ParseFrom(bizarroData); TestEmptyMessage emptyMessage = TestEmptyMessage.ParseFrom(bizarroData); // All fields should have been interpreted as unknown, so the debug strings // should be the same. Assert.AreEqual(emptyMessage.ToString(), allTypesMessage.ToString()); }
public void SerializeExtensions() { // TestAllTypes and TestAllExtensions should have compatible wire formats, // so if we serialize a TestAllExtensions then parse it as TestAllTypes // it should work. TestAllExtensions message = TestUtil.GetAllExtensionsSet(); ByteString rawBytes = message.ToByteString(); Assert.AreEqual(rawBytes.Length, message.SerializedSize); TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes); TestUtil.AssertAllFieldsSet(message2); }
public void DynamicMessageSerialization() { IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor); reflectionTester.SetAllFieldsViaReflection(builder); IMessage message = builder.WeakBuild(); ByteString rawBytes = message.ToByteString(); TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes); TestUtil.AssertAllFieldsSet(message2); // In fact, the serialized forms should be exactly the same, byte-for-byte. Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes); }
public void ParseUnknownEnumValue() { FieldDescriptor singularField = TestAllTypes.Descriptor.FindDescriptor <FieldDescriptor>("optional_nested_enum"); FieldDescriptor repeatedField = TestAllTypes.Descriptor.FindDescriptor <FieldDescriptor>("repeated_nested_enum"); Assert.IsNotNull(singularField); Assert.IsNotNull(repeatedField); ByteString data = UnknownFieldSet.CreateBuilder() .AddField(singularField.FieldNumber, UnknownField.CreateBuilder() .AddVarint((int)TestAllTypes.Types.NestedEnum.BAR) .AddVarint(5) // not valid .Build()) .AddField(repeatedField.FieldNumber, UnknownField.CreateBuilder() .AddVarint((int)TestAllTypes.Types.NestedEnum.FOO) .AddVarint(4) // not valid .AddVarint((int)TestAllTypes.Types.NestedEnum.BAZ) .AddVarint(6) // not valid .Build()) .Build() .ToByteString(); { TestAllTypes message = TestAllTypes.ParseFrom(data); Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.OptionalNestedEnum); TestUtil.AssertEqual(new[] { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.BAZ }, message.RepeatedNestedEnumList); TestUtil.AssertEqual(new[] { 5UL }, message.UnknownFields[singularField.FieldNumber].VarintList); TestUtil.AssertEqual(new[] { 4UL, 6UL }, message.UnknownFields[repeatedField.FieldNumber].VarintList); } { TestAllExtensions message = TestAllExtensions.ParseFrom(data, TestUtil.CreateExtensionRegistry()); Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, message.GetExtension(UnitTestProtoFile.OptionalNestedEnumExtension)); TestUtil.AssertEqual(new[] { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.BAZ }, message.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension)); TestUtil.AssertEqual(new[] { 5UL }, message.UnknownFields[singularField.FieldNumber].VarintList); TestUtil.AssertEqual(new[] { 4UL, 6UL }, message.UnknownFields[repeatedField.FieldNumber].VarintList); } }
public void SizeLimit() { // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't // apply to the latter case. MemoryStream ms = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray()); CodedInputStream input = CodedInputStream.CreateInstance(ms); input.SetSizeLimit(16); try { TestAllTypes.ParseFrom(input); Assert.Fail("Should have thrown an exception!"); } catch (InvalidProtocolBufferException) { // success. } }
public void TestSerializeAndParse() { TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); builder.SetOptionalInt32(1234); builder.SetOptionalString("hello"); builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance); ByteString data = builder.Build().ToByteString(); TestAllTypes message = TestAllTypes.ParseFrom(data); Assert.AreEqual(1234, message.OptionalInt32); Assert.AreEqual("hello", message.OptionalString); Assert.AreEqual(ByteString.Empty, message.OptionalBytes); Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum); Assert.IsTrue(message.HasOptionalNestedMessage); Assert.AreEqual(0, message.OptionalNestedMessage.Bb); }
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 ParseKnownAndUnknown() { // Test mixing known and unknown fields when parsing. UnknownFieldSet fields = UnknownFieldSet.CreateBuilder(unknownFields) .AddField(123456, UnknownField.CreateBuilder().AddVarint(654321).Build()) .Build(); ByteString data = fields.ToByteString(); TestAllTypes destination = TestAllTypes.ParseFrom(data); TestUtil.AssertAllFieldsSet(destination); Assert.AreEqual(1, destination.UnknownFields.FieldDictionary.Count); UnknownField field = destination.UnknownFields[123456]; Assert.AreEqual(1, field.VarintList.Count); Assert.AreEqual(654321, (long)field.VarintList[0]); }