Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. |
| 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | using System.Reflection; |
| 17 | using Google.ProtocolBuffers.Descriptors; |
| 18 | using Google.ProtocolBuffers.TestProtos; |
| 19 | using NUnit.Framework; |
| 20 | |
| 21 | namespace Google.ProtocolBuffers { |
| 22 | [TestFixture] |
| 23 | public class WireFormatTest { |
| 24 | |
| 25 | /// <summary> |
| 26 | /// Keeps the attributes on FieldType and the switch statement in WireFormat in sync. |
| 27 | /// </summary> |
| 28 | [Test] |
| 29 | public void FieldTypeToWireTypeMapping() { |
| 30 | foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) { |
| 31 | FieldType fieldType = (FieldType)field.GetValue(null); |
| 32 | FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0]; |
| 33 | Assert.AreEqual(mapping.WireType, WireFormat.GetWireType(fieldType)); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | [Test] |
| 38 | public void Serialization() { |
| 39 | TestAllTypes message = TestUtil.GetAllSet(); |
| 40 | |
| 41 | ByteString rawBytes = message.ToByteString(); |
| 42 | Assert.AreEqual(rawBytes.Length, message.SerializedSize); |
| 43 | |
| 44 | TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes); |
| 45 | |
| 46 | TestUtil.AssertAllFieldsSet(message2); |
| 47 | } |
| 48 | |
| 49 | [Test] |
| 50 | public void SerializeExtensions() { |
| 51 | // TestAllTypes and TestAllExtensions should have compatible wire formats, |
| 52 | // so if we serealize a TestAllExtensions then parse it as TestAllTypes |
| 53 | // it should work. |
| 54 | |
| 55 | TestAllExtensions message = TestUtil.GetAllExtensionsSet(); |
| 56 | ByteString rawBytes = message.ToByteString(); |
| 57 | Assert.AreEqual(rawBytes.Length, message.SerializedSize); |
| 58 | |
| 59 | TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes); |
| 60 | |
| 61 | TestUtil.AssertAllFieldsSet(message2); |
| 62 | } |
| 63 | |
| 64 | [Test] |
| 65 | public void ParseExtensions() { |
| 66 | // TestAllTypes and TestAllExtensions should have compatible wire formats, |
| 67 | // so if we serealize a TestAllTypes then parse it as TestAllExtensions |
| 68 | // it should work. |
| 69 | |
| 70 | TestAllTypes message = TestUtil.GetAllSet(); |
| 71 | ByteString rawBytes = message.ToByteString(); |
| 72 | |
| 73 | ExtensionRegistry registry = ExtensionRegistry.CreateInstance(); |
| 74 | TestUtil.RegisterAllExtensions(registry); |
| 75 | registry = registry.AsReadOnly(); |
| 76 | |
| 77 | TestAllExtensions message2 = |
| 78 | TestAllExtensions.ParseFrom(rawBytes, registry); |
| 79 | |
| 80 | TestUtil.AssertAllExtensionsSet(message2); |
| 81 | } |
| 82 | |
| 83 | [Test] |
| 84 | public void ExtensionsSerializedSize() { |
| 85 | Assert.AreEqual(TestUtil.GetAllSet().SerializedSize, TestUtil.GetAllExtensionsSet().SerializedSize); |
| 86 | } |
| 87 | |
| 88 | private void AssertFieldsInOrder(ByteString data) { |
| 89 | CodedInputStream input = data.CreateCodedInput(); |
| 90 | uint previousTag = 0; |
| 91 | |
| 92 | while (true) { |
| 93 | uint tag = input.ReadTag(); |
| 94 | if (tag == 0) { |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | Assert.IsTrue(tag > previousTag); |
| 99 | previousTag = tag; |
| 100 | input.SkipField(tag); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | [Test] |
| 105 | public void InterleavedFieldsAndExtensions() { |
| 106 | // Tests that fields are written in order even when extension ranges |
| 107 | // are interleaved with field numbers. |
| 108 | ByteString data = |
| 109 | TestFieldOrderings.CreateBuilder() |
| 110 | .SetMyInt(1) |
| 111 | .SetMyString("foo") |
| 112 | .SetMyFloat(1.0F) |
| 113 | .SetExtension(UnitTestProtoFile.MyExtensionInt, 23) |
| 114 | .SetExtension(UnitTestProtoFile.MyExtensionString, "bar") |
| 115 | .Build().ToByteString(); |
| 116 | AssertFieldsInOrder(data); |
| 117 | |
| 118 | MessageDescriptor descriptor = TestFieldOrderings.Descriptor; |
| 119 | ByteString dynamic_data = |
| 120 | DynamicMessage.CreateBuilder(TestFieldOrderings.Descriptor) |
| 121 | .SetField(descriptor.FindDescriptor<FieldDescriptor>("my_int"), 1L) |
| 122 | .SetField(descriptor.FindDescriptor<FieldDescriptor>("my_string"), "foo") |
| 123 | .SetField(descriptor.FindDescriptor<FieldDescriptor>("my_float"), 1.0F) |
| 124 | .SetField(UnitTestProtoFile.MyExtensionInt.Descriptor, 23) |
| 125 | .SetField(UnitTestProtoFile.MyExtensionString.Descriptor, "bar") |
| 126 | .WeakBuild().ToByteString(); |
| 127 | AssertFieldsInOrder(dynamic_data); |
| 128 | } |
| 129 | |
| 130 | private const int UnknownTypeId = 1550055; |
| 131 | private static readonly int TypeId1 = TestMessageSetExtension1.Descriptor.Extensions[0].FieldNumber; |
| 132 | private static readonly int TypeId2 = TestMessageSetExtension2.Descriptor.Extensions[0].FieldNumber; |
| 133 | |
| 134 | [Test] |
| 135 | public void SerializeMessageSet() { |
| 136 | // Set up a TestMessageSet with two known messages and an unknown one. |
| 137 | TestMessageSet messageSet = |
| 138 | TestMessageSet.CreateBuilder() |
| 139 | .SetExtension( |
| 140 | TestMessageSetExtension1.MessageSetExtension, |
| 141 | TestMessageSetExtension1.CreateBuilder().SetI(123).Build()) |
| 142 | .SetExtension( |
| 143 | TestMessageSetExtension2.MessageSetExtension, |
| 144 | TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build()) |
| 145 | .SetUnknownFields( |
| 146 | UnknownFieldSet.CreateBuilder() |
| 147 | .AddField(UnknownTypeId, |
| 148 | UnknownField.CreateBuilder() |
| 149 | .AddLengthDelimited(ByteString.CopyFromUtf8("bar")) |
| 150 | .Build()) |
| 151 | .Build()) |
| 152 | .Build(); |
| 153 | |
| 154 | ByteString data = messageSet.ToByteString(); |
| 155 | |
| 156 | // Parse back using RawMessageSet and check the contents. |
| 157 | RawMessageSet raw = RawMessageSet.ParseFrom(data); |
| 158 | |
| 159 | Assert.AreEqual(0, raw.UnknownFields.FieldDictionary.Count); |
| 160 | |
| 161 | Assert.AreEqual(3, raw.ItemCount); |
| 162 | Assert.AreEqual(TypeId1, raw.ItemList[0].TypeId); |
| 163 | Assert.AreEqual(TypeId2, raw.ItemList[1].TypeId); |
| 164 | Assert.AreEqual(UnknownTypeId, raw.ItemList[2].TypeId); |
| 165 | |
| 166 | TestMessageSetExtension1 message1 = TestMessageSetExtension1.ParseFrom(raw.GetItem(0).Message.ToByteArray()); |
| 167 | Assert.AreEqual(123, message1.I); |
| 168 | |
| 169 | TestMessageSetExtension2 message2 = TestMessageSetExtension2.ParseFrom(raw.GetItem(1).Message.ToByteArray()); |
| 170 | Assert.AreEqual("foo", message2.Str); |
| 171 | |
| 172 | Assert.AreEqual("bar", raw.GetItem(2).Message.ToStringUtf8()); |
| 173 | } |
| 174 | |
| 175 | [Test] |
| 176 | public void ParseMessageSet() { |
| 177 | ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance(); |
| 178 | extensionRegistry.Add(TestMessageSetExtension1.MessageSetExtension); |
| 179 | extensionRegistry.Add(TestMessageSetExtension2.MessageSetExtension); |
| 180 | |
| 181 | // Set up a RawMessageSet with two known messages and an unknown one. |
| 182 | RawMessageSet raw = |
| 183 | RawMessageSet.CreateBuilder() |
| 184 | .AddItem( |
| 185 | RawMessageSet.Types.Item.CreateBuilder() |
| 186 | .SetTypeId(TypeId1) |
| 187 | .SetMessage( |
| 188 | TestMessageSetExtension1.CreateBuilder() |
| 189 | .SetI(123) |
| 190 | .Build().ToByteString()) |
| 191 | .Build()) |
| 192 | .AddItem( |
| 193 | RawMessageSet.Types.Item.CreateBuilder() |
| 194 | .SetTypeId(TypeId2) |
| 195 | .SetMessage( |
| 196 | TestMessageSetExtension2.CreateBuilder() |
| 197 | .SetStr("foo") |
| 198 | .Build().ToByteString()) |
| 199 | .Build()) |
| 200 | .AddItem( |
| 201 | RawMessageSet.Types.Item.CreateBuilder() |
| 202 | .SetTypeId(UnknownTypeId) |
| 203 | .SetMessage(ByteString.CopyFromUtf8("bar")) |
| 204 | .Build()) |
| 205 | .Build(); |
| 206 | |
| 207 | ByteString data = raw.ToByteString(); |
| 208 | |
| 209 | // Parse as a TestMessageSet and check the contents. |
| 210 | TestMessageSet messageSet = |
| 211 | TestMessageSet.ParseFrom(data, extensionRegistry); |
| 212 | |
| 213 | Assert.AreEqual(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I); |
| 214 | Assert.AreEqual("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str); |
| 215 | |
| 216 | // Check for unknown field with type LENGTH_DELIMITED, |
| 217 | // number UNKNOWN_TYPE_ID, and contents "bar". |
| 218 | UnknownFieldSet unknownFields = messageSet.UnknownFields; |
| 219 | Assert.AreEqual(1, unknownFields.FieldDictionary.Count); |
| 220 | Assert.IsTrue(unknownFields.HasField(UnknownTypeId)); |
| 221 | |
| 222 | UnknownField field = unknownFields[UnknownTypeId]; |
| 223 | Assert.AreEqual(1, field.LengthDelimitedList.Count); |
| 224 | Assert.AreEqual("bar", field.LengthDelimitedList[0].ToStringUtf8()); |
| 225 | } |
| 226 | |
| 227 | } |
| 228 | } |