LegacyGeneratedCodeTest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. using Google.Protobuf;
  33. using Google.Protobuf.Reflection;
  34. using System.Buffers;
  35. using pb = global::Google.Protobuf;
  36. using pbr = global::Google.Protobuf.Reflection;
  37. using NUnit.Framework;
  38. namespace Google.Protobuf
  39. {
  40. public class LegacyGeneratedCodeTest
  41. {
  42. [Test]
  43. public void IntermixingOfNewAndLegacyGeneratedCodeWorksWithCodedInputStream()
  44. {
  45. var message = new ParseContextEnabledMessageB
  46. {
  47. A = new LegacyGeneratedCodeMessageA
  48. {
  49. Bb = new ParseContextEnabledMessageB { OptionalInt32 = 12345 }
  50. },
  51. OptionalInt32 = 6789
  52. };
  53. var data = message.ToByteArray();
  54. // when parsing started using CodedInputStream and a message with legacy generated code
  55. // is encountered somewhere in the parse tree, we still need to be able to use its
  56. // MergeFrom(CodedInputStream) method to parse correctly.
  57. var codedInput = new CodedInputStream(data);
  58. var parsed = new ParseContextEnabledMessageB();
  59. codedInput.ReadRawMessage(parsed);
  60. Assert.IsTrue(codedInput.IsAtEnd);
  61. Assert.AreEqual(12345, parsed.A.Bb.OptionalInt32);
  62. Assert.AreEqual(6789, parsed.OptionalInt32);
  63. }
  64. [Test]
  65. public void LegacyGeneratedCodeThrowsWithReadOnlySequence()
  66. {
  67. var message = new ParseContextEnabledMessageB
  68. {
  69. A = new LegacyGeneratedCodeMessageA
  70. {
  71. Bb = new ParseContextEnabledMessageB { OptionalInt32 = 12345 }
  72. },
  73. OptionalInt32 = 6789
  74. };
  75. var data = message.ToByteArray();
  76. // if parsing started using ReadOnlySequence and we don't have a CodedInputStream
  77. // instance at hand, we cannot fall back to the legacy MergeFrom(CodedInputStream)
  78. // method and parsing will fail. As a consequence, one can only use parsing
  79. // from ReadOnlySequence if all the messages in the parsing tree have their generated
  80. // code up to date.
  81. var exception = Assert.Throws<InvalidProtocolBufferException>(() =>
  82. {
  83. ParseContext.Initialize(new ReadOnlySequence<byte>(data), out ParseContext parseCtx);
  84. var parsed = new ParseContextEnabledMessageB();
  85. ParsingPrimitivesMessages.ReadRawMessage(ref parseCtx, parsed);
  86. });
  87. Assert.AreEqual($"Message {typeof(LegacyGeneratedCodeMessageA).Name} doesn't provide the generated method that enables ParseContext-based parsing. You might need to regenerate the generated protobuf code.", exception.Message);
  88. }
  89. // hand-modified version of a generated message that only provides the legacy
  90. // MergeFrom(CodedInputStream) method and doesn't implement IBufferMessage.
  91. private sealed partial class LegacyGeneratedCodeMessageA : pb::IMessage {
  92. private pb::UnknownFieldSet _unknownFields;
  93. pbr::MessageDescriptor pb::IMessage.Descriptor => throw new System.NotImplementedException();
  94. /// <summary>Field number for the "bb" field.</summary>
  95. public const int BbFieldNumber = 1;
  96. private ParseContextEnabledMessageB bb_;
  97. public ParseContextEnabledMessageB Bb {
  98. get { return bb_; }
  99. set {
  100. bb_ = value;
  101. }
  102. }
  103. public void WriteTo(pb::CodedOutputStream output) {
  104. if (bb_ != null) {
  105. output.WriteRawTag(10);
  106. output.WriteMessage(Bb);
  107. }
  108. if (_unknownFields != null) {
  109. _unknownFields.WriteTo(output);
  110. }
  111. }
  112. public int CalculateSize() {
  113. int size = 0;
  114. if (bb_ != null) {
  115. size += 1 + pb::CodedOutputStream.ComputeMessageSize(Bb);
  116. }
  117. if (_unknownFields != null) {
  118. size += _unknownFields.CalculateSize();
  119. }
  120. return size;
  121. }
  122. public void MergeFrom(pb::CodedInputStream input) {
  123. uint tag;
  124. while ((tag = input.ReadTag()) != 0) {
  125. switch(tag) {
  126. default:
  127. _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
  128. break;
  129. case 10: {
  130. if (bb_ == null) {
  131. Bb = new ParseContextEnabledMessageB();
  132. }
  133. input.ReadMessage(Bb);
  134. break;
  135. }
  136. }
  137. }
  138. }
  139. }
  140. // hand-modified version of a generated message that does provide
  141. // the new InternalMergeFrom(ref ParseContext) method.
  142. private sealed partial class ParseContextEnabledMessageB : pb::IBufferMessage {
  143. private pb::UnknownFieldSet _unknownFields;
  144. pbr::MessageDescriptor pb::IMessage.Descriptor => throw new System.NotImplementedException();
  145. /// <summary>Field number for the "a" field.</summary>
  146. public const int AFieldNumber = 1;
  147. private LegacyGeneratedCodeMessageA a_;
  148. public LegacyGeneratedCodeMessageA A {
  149. get { return a_; }
  150. set {
  151. a_ = value;
  152. }
  153. }
  154. /// <summary>Field number for the "optional_int32" field.</summary>
  155. public const int OptionalInt32FieldNumber = 2;
  156. private int optionalInt32_;
  157. public int OptionalInt32 {
  158. get { return optionalInt32_; }
  159. set {
  160. optionalInt32_ = value;
  161. }
  162. }
  163. public void WriteTo(pb::CodedOutputStream output) {
  164. if (a_ != null) {
  165. output.WriteRawTag(10);
  166. output.WriteMessage(A);
  167. }
  168. if (OptionalInt32 != 0) {
  169. output.WriteRawTag(16);
  170. output.WriteInt32(OptionalInt32);
  171. }
  172. if (_unknownFields != null) {
  173. _unknownFields.WriteTo(output);
  174. }
  175. }
  176. public int CalculateSize() {
  177. int size = 0;
  178. if (a_ != null) {
  179. size += 1 + pb::CodedOutputStream.ComputeMessageSize(A);
  180. }
  181. if (OptionalInt32 != 0) {
  182. size += 1 + pb::CodedOutputStream.ComputeInt32Size(OptionalInt32);
  183. }
  184. if (_unknownFields != null) {
  185. size += _unknownFields.CalculateSize();
  186. }
  187. return size;
  188. }
  189. public void MergeFrom(pb::CodedInputStream input) {
  190. input.ReadRawMessage(this);
  191. }
  192. void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
  193. uint tag;
  194. while ((tag = input.ReadTag()) != 0) {
  195. switch(tag) {
  196. default:
  197. _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
  198. break;
  199. case 10: {
  200. if (a_ == null) {
  201. A = new LegacyGeneratedCodeMessageA();
  202. }
  203. input.ReadMessage(A);
  204. break;
  205. }
  206. case 16: {
  207. OptionalInt32 = input.ReadInt32();
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }