1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2008 Google Inc. All rights reserved. 4 // https://developers.google.com/protocol-buffers/ 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 #endregion 32 33 using System; 34 using System.IO; 35 using Google.Protobuf.TestProtos; 36 using NUnit.Framework; 37 38 namespace Google.Protobuf 39 { 40 public class UnknownFieldSetTest 41 { 42 [Test] EmptyUnknownFieldSet()43 public void EmptyUnknownFieldSet() 44 { 45 UnknownFieldSet unknownFields = new UnknownFieldSet(); 46 Assert.AreEqual(0, unknownFields.CalculateSize()); 47 } 48 49 [Test] MergeUnknownFieldSet()50 public void MergeUnknownFieldSet() 51 { 52 UnknownFieldSet unknownFields = new UnknownFieldSet(); 53 UnknownField field = new UnknownField(); 54 field.AddFixed32(123); 55 unknownFields.AddOrReplaceField(1, field); 56 UnknownFieldSet otherUnknownFields = new UnknownFieldSet(); 57 Assert.IsFalse(otherUnknownFields.HasField(1)); 58 UnknownFieldSet.MergeFrom(otherUnknownFields, unknownFields); 59 Assert.IsTrue(otherUnknownFields.HasField(1)); 60 } 61 62 [Test] TestMergeCodedInput()63 public void TestMergeCodedInput() 64 { 65 var message = SampleMessages.CreateFullTestAllTypes(); 66 var emptyMessage = new TestEmptyMessage(); 67 emptyMessage.MergeFrom(message.ToByteArray()); 68 Assert.AreEqual(message.CalculateSize(), emptyMessage.CalculateSize()); 69 Assert.AreEqual(message.ToByteArray(), emptyMessage.ToByteArray()); 70 71 var newMessage = new TestAllTypes(); 72 newMessage.MergeFrom(emptyMessage.ToByteArray()); 73 Assert.AreEqual(message, newMessage); 74 Assert.AreEqual(message.CalculateSize(), newMessage.CalculateSize()); 75 } 76 77 [Test] TestMergeMessage()78 public void TestMergeMessage() 79 { 80 var message = SampleMessages.CreateFullTestAllTypes(); 81 var emptyMessage = new TestEmptyMessage(); 82 var otherEmptyMessage = new TestEmptyMessage(); 83 emptyMessage.MergeFrom(message.ToByteArray()); 84 otherEmptyMessage.MergeFrom(emptyMessage); 85 86 Assert.AreEqual(message.CalculateSize(), otherEmptyMessage.CalculateSize()); 87 Assert.AreEqual(message.ToByteArray(), otherEmptyMessage.ToByteArray()); 88 } 89 90 [Test] TestEquals()91 public void TestEquals() 92 { 93 var message = SampleMessages.CreateFullTestAllTypes(); 94 var emptyMessage = new TestEmptyMessage(); 95 var otherEmptyMessage = new TestEmptyMessage(); 96 Assert.AreEqual(emptyMessage, otherEmptyMessage); 97 emptyMessage.MergeFrom(message.ToByteArray()); 98 Assert.AreNotEqual(emptyMessage.CalculateSize(), 99 otherEmptyMessage.CalculateSize()); 100 Assert.AreNotEqual(emptyMessage, otherEmptyMessage); 101 } 102 103 [Test] TestHashCode()104 public void TestHashCode() 105 { 106 var message = SampleMessages.CreateFullTestAllTypes(); 107 var emptyMessage = new TestEmptyMessage(); 108 int hashCode = emptyMessage.GetHashCode(); 109 emptyMessage.MergeFrom(message.ToByteArray()); 110 Assert.AreNotEqual(hashCode, emptyMessage.GetHashCode()); 111 } 112 113 [Test] TestClone()114 public void TestClone() 115 { 116 var emptyMessage = new TestEmptyMessage(); 117 var otherEmptyMessage = new TestEmptyMessage(); 118 otherEmptyMessage = emptyMessage.Clone(); 119 Assert.AreEqual(emptyMessage.CalculateSize(), otherEmptyMessage.CalculateSize()); 120 Assert.AreEqual(emptyMessage.ToByteArray(), otherEmptyMessage.ToByteArray()); 121 122 var message = SampleMessages.CreateFullTestAllTypes(); 123 emptyMessage.MergeFrom(message.ToByteArray()); 124 otherEmptyMessage = emptyMessage.Clone(); 125 Assert.AreEqual(message.CalculateSize(), otherEmptyMessage.CalculateSize()); 126 Assert.AreEqual(message.ToByteArray(), otherEmptyMessage.ToByteArray()); 127 } 128 129 [Test] TestDiscardUnknownFields()130 public void TestDiscardUnknownFields() 131 { 132 var message = SampleMessages.CreateFullTestAllTypes(); 133 var goldenEmptyMessage = new TestEmptyMessage(); 134 byte[] data = message.ToByteArray(); 135 int fullSize = message.CalculateSize(); 136 137 Action<IMessage> assertEmpty = msg => 138 { 139 Assert.AreEqual(0, msg.CalculateSize()); 140 Assert.AreEqual(goldenEmptyMessage, msg); 141 }; 142 143 Action<IMessage> assertFull = msg => Assert.AreEqual(fullSize, msg.CalculateSize()); 144 145 // Test the behavior of the parsers with and without discarding, both generic and non-generic. 146 MessageParser<TestEmptyMessage> retainingParser1 = TestEmptyMessage.Parser; 147 MessageParser retainingParser2 = retainingParser1; 148 MessageParser<TestEmptyMessage> discardingParser1 = retainingParser1.WithDiscardUnknownFields(true); 149 MessageParser discardingParser2 = retainingParser2.WithDiscardUnknownFields(true); 150 151 // Test parse from byte[] 152 assertFull(retainingParser1.ParseFrom(data)); 153 assertFull(retainingParser2.ParseFrom(data)); 154 assertEmpty(discardingParser1.ParseFrom(data)); 155 assertEmpty(discardingParser2.ParseFrom(data)); 156 157 // Test parse from byte[] with offset 158 assertFull(retainingParser1.ParseFrom(data, 0, data.Length)); 159 assertFull(retainingParser2.ParseFrom(data, 0, data.Length)); 160 assertEmpty(discardingParser1.ParseFrom(data, 0, data.Length)); 161 assertEmpty(discardingParser2.ParseFrom(data, 0, data.Length)); 162 163 // Test parse from CodedInputStream 164 assertFull(retainingParser1.ParseFrom(new CodedInputStream(data))); 165 assertFull(retainingParser2.ParseFrom(new CodedInputStream(data))); 166 assertEmpty(discardingParser1.ParseFrom(new CodedInputStream(data))); 167 assertEmpty(discardingParser2.ParseFrom(new CodedInputStream(data))); 168 169 // Test parse from Stream 170 assertFull(retainingParser1.ParseFrom(new MemoryStream(data))); 171 assertFull(retainingParser2.ParseFrom(new MemoryStream(data))); 172 assertEmpty(discardingParser1.ParseFrom(new MemoryStream(data))); 173 assertEmpty(discardingParser2.ParseFrom(new MemoryStream(data))); 174 } 175 176 [Test] TestReadInvalidWireTypeThrowsInvalidProtocolBufferException()177 public void TestReadInvalidWireTypeThrowsInvalidProtocolBufferException() 178 { 179 MemoryStream ms = new MemoryStream(); 180 CodedOutputStream output = new CodedOutputStream(ms); 181 182 uint tag = WireFormat.MakeTag(1, (WireFormat.WireType)6); 183 output.WriteRawVarint32(tag); 184 output.WriteLength(-1); 185 output.Flush(); 186 ms.Position = 0; 187 188 CodedInputStream input = new CodedInputStream(ms); 189 Assert.AreEqual(tag, input.ReadTag()); 190 191 Assert.Throws<InvalidProtocolBufferException>(() => UnknownFieldSet.MergeFieldFrom(null, input)); 192 } 193 } 194 } 195