• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using Google.Protobuf.Reflection;
11 using UnitTest.Issues.TestProtos;
12 using NUnit.Framework;
13 using System.IO;
14 using static UnitTest.Issues.TestProtos.OneofMerging.Types;
15 
16 namespace Google.Protobuf
17 {
18     /// <summary>
19     /// Tests for issues which aren't easily compartmentalized into other unit tests.
20     /// </summary>
21     public class IssuesTest
22     {
23         // Issue 45
24         [Test]
FieldCalledItem()25         public void FieldCalledItem()
26         {
27             ItemField message = new ItemField { Item = 3 };
28             FieldDescriptor field = ItemField.Descriptor.FindFieldByName("item");
29             Assert.NotNull(field);
30             Assert.AreEqual(3, (int)field.Accessor.GetValue(message));
31         }
32 
33         [Test]
ReservedNames()34         public void ReservedNames()
35         {
36             var message = new ReservedNames { Types_ = 10, Descriptor_ = 20 };
37             // Underscores aren't reflected in the JSON.
38             Assert.AreEqual("{ \"types\": 10, \"descriptor\": 20 }", message.ToString());
39         }
40 
41         [Test]
JsonNameParseTest()42         public void JsonNameParseTest()
43         {
44             var settings = new JsonParser.Settings(10, TypeRegistry.FromFiles(UnittestIssuesReflection.Descriptor));
45             var parser = new JsonParser(settings);
46 
47             // It is safe to use either original field name or explicitly specified json_name
48             Assert.AreEqual(new TestJsonName { Name = "test", Description = "test2", Guid = "test3" },
49                 parser.Parse<TestJsonName>("{ \"name\": \"test\", \"desc\": \"test2\", \"guid\": \"test3\" }"));
50         }
51 
52         [Test]
JsonNameFormatTest()53         public void JsonNameFormatTest()
54         {
55             var message = new TestJsonName { Name = "test", Description = "test2", Guid = "test3" };
56             Assert.AreEqual("{ \"name\": \"test\", \"desc\": \"test2\", \"exid\": \"test3\" }",
57                 JsonFormatter.Default.Format(message));
58         }
59 
60         [Test]
OneofMerging()61         public void OneofMerging()
62         {
63             var message1 = new OneofMerging { Nested = new Nested { X = 10 } };
64             var message2 = new OneofMerging { Nested = new Nested { Y = 20 } };
65             var expected = new OneofMerging { Nested = new Nested { X = 10, Y = 20 } };
66 
67             var merged = message1.Clone();
68             merged.MergeFrom(message2);
69             Assert.AreEqual(expected, merged);
70         }
71 
72         // Check that a tag immediately followed by end of limit can still be read.
73         [Test]
CodedInputStream_LimitReachedRightAfterTag()74         public void CodedInputStream_LimitReachedRightAfterTag()
75         {
76             MemoryStream ms = new MemoryStream();
77             var cos = new CodedOutputStream(ms);
78             cos.WriteTag(11, WireFormat.WireType.Varint);
79             Assert.AreEqual(1, cos.Position);
80             cos.WriteString("some extra padding");  // ensure is currentLimit distinct from the end of the buffer.
81             cos.Flush();
82 
83             var cis = new CodedInputStream(ms.ToArray());
84             cis.PushLimit(1);  // make sure we reach the limit right after reading the tag.
85 
86             // we still must read the tag correctly, even though the tag is at the very end of our limited input
87             // (which is a corner case and will most likely result in an error when trying to read value of the field
88             // described by this tag, but it would be a logical error not to read the tag that's actually present).
89             // See https://github.com/protocolbuffers/protobuf/pull/7289
90             cis.AssertNextTag(WireFormat.MakeTag(11, WireFormat.WireType.Varint));
91         }
92 
93         [Test]
NoneFieldInOneof()94         public void NoneFieldInOneof()
95         {
96             var message = new OneofWithNoneField();
97             var emptyHashCode = message.GetHashCode();
98             Assert.AreEqual(OneofWithNoneField.TestOneofCase.None, message.TestCase);
99             message.None = "test";
100             Assert.AreEqual(OneofWithNoneField.TestOneofCase.None_, message.TestCase);
101             Assert.AreNotEqual(emptyHashCode, message.GetHashCode());
102 
103             var bytes = message.ToByteArray();
104             var parsed = OneofWithNoneField.Parser.ParseFrom(bytes);
105             Assert.AreEqual(message, parsed);
106             Assert.AreEqual("test", parsed.None);
107         }
108     }
109 }
110