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