LegacyGeneratedCodeTest.cs 12 KB

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