GeneratedMessageTest.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 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 System;
  33. using System.IO;
  34. using Google.Protobuf.TestProtos;
  35. using NUnit.Framework;
  36. using System.Collections;
  37. using System.Collections.Generic;
  38. using System.Linq;
  39. using Google.Protobuf.WellKnownTypes;
  40. namespace Google.Protobuf
  41. {
  42. /// <summary>
  43. /// Tests around the generated TestAllTypes message.
  44. /// </summary>
  45. public class GeneratedMessageTest
  46. {
  47. [Test]
  48. public void EmptyMessageFieldDistinctFromMissingMessageField()
  49. {
  50. // This demonstrates what we're really interested in...
  51. var message1 = new TestAllTypes { SingleForeignMessage = new ForeignMessage() };
  52. var message2 = new TestAllTypes(); // SingleForeignMessage is null
  53. EqualityTester.AssertInequality(message1, message2);
  54. }
  55. [Test]
  56. public void DefaultValues()
  57. {
  58. // Single fields
  59. var message = new TestAllTypes();
  60. Assert.AreEqual(false, message.SingleBool);
  61. Assert.AreEqual(ByteString.Empty, message.SingleBytes);
  62. Assert.AreEqual(0.0, message.SingleDouble);
  63. Assert.AreEqual(0, message.SingleFixed32);
  64. Assert.AreEqual(0L, message.SingleFixed64);
  65. Assert.AreEqual(0.0f, message.SingleFloat);
  66. Assert.AreEqual(ForeignEnum.ForeignUnspecified, message.SingleForeignEnum);
  67. Assert.IsNull(message.SingleForeignMessage);
  68. Assert.AreEqual(ImportEnum.Unspecified, message.SingleImportEnum);
  69. Assert.IsNull(message.SingleImportMessage);
  70. Assert.AreEqual(0, message.SingleInt32);
  71. Assert.AreEqual(0L, message.SingleInt64);
  72. Assert.AreEqual(TestAllTypes.Types.NestedEnum.Unspecified, message.SingleNestedEnum);
  73. Assert.IsNull(message.SingleNestedMessage);
  74. Assert.IsNull(message.SinglePublicImportMessage);
  75. Assert.AreEqual(0, message.SingleSfixed32);
  76. Assert.AreEqual(0L, message.SingleSfixed64);
  77. Assert.AreEqual(0, message.SingleSint32);
  78. Assert.AreEqual(0L, message.SingleSint64);
  79. Assert.AreEqual("", message.SingleString);
  80. Assert.AreEqual(0U, message.SingleUint32);
  81. Assert.AreEqual(0UL, message.SingleUint64);
  82. // Repeated fields
  83. Assert.AreEqual(0, message.RepeatedBool.Count);
  84. Assert.AreEqual(0, message.RepeatedBytes.Count);
  85. Assert.AreEqual(0, message.RepeatedDouble.Count);
  86. Assert.AreEqual(0, message.RepeatedFixed32.Count);
  87. Assert.AreEqual(0, message.RepeatedFixed64.Count);
  88. Assert.AreEqual(0, message.RepeatedFloat.Count);
  89. Assert.AreEqual(0, message.RepeatedForeignEnum.Count);
  90. Assert.AreEqual(0, message.RepeatedForeignMessage.Count);
  91. Assert.AreEqual(0, message.RepeatedImportEnum.Count);
  92. Assert.AreEqual(0, message.RepeatedImportMessage.Count);
  93. Assert.AreEqual(0, message.RepeatedNestedEnum.Count);
  94. Assert.AreEqual(0, message.RepeatedNestedMessage.Count);
  95. Assert.AreEqual(0, message.RepeatedPublicImportMessage.Count);
  96. Assert.AreEqual(0, message.RepeatedSfixed32.Count);
  97. Assert.AreEqual(0, message.RepeatedSfixed64.Count);
  98. Assert.AreEqual(0, message.RepeatedSint32.Count);
  99. Assert.AreEqual(0, message.RepeatedSint64.Count);
  100. Assert.AreEqual(0, message.RepeatedString.Count);
  101. Assert.AreEqual(0, message.RepeatedUint32.Count);
  102. Assert.AreEqual(0, message.RepeatedUint64.Count);
  103. // Oneof fields
  104. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
  105. Assert.AreEqual(0, message.OneofUint32);
  106. Assert.AreEqual("", message.OneofString);
  107. Assert.AreEqual(ByteString.Empty, message.OneofBytes);
  108. Assert.IsNull(message.OneofNestedMessage);
  109. }
  110. [Test]
  111. public void NullStringAndBytesRejected()
  112. {
  113. var message = new TestAllTypes();
  114. Assert.Throws<ArgumentNullException>(() => message.SingleString = null);
  115. Assert.Throws<ArgumentNullException>(() => message.OneofString = null);
  116. Assert.Throws<ArgumentNullException>(() => message.SingleBytes = null);
  117. Assert.Throws<ArgumentNullException>(() => message.OneofBytes = null);
  118. }
  119. [Test]
  120. public void RoundTrip_Empty()
  121. {
  122. var message = new TestAllTypes();
  123. // Without setting any values, there's nothing to write.
  124. byte[] bytes = message.ToByteArray();
  125. Assert.AreEqual(0, bytes.Length);
  126. TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);
  127. Assert.AreEqual(message, parsed);
  128. }
  129. [Test]
  130. public void RoundTrip_SingleValues()
  131. {
  132. var message = new TestAllTypes
  133. {
  134. SingleBool = true,
  135. SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
  136. SingleDouble = 23.5,
  137. SingleFixed32 = 23,
  138. SingleFixed64 = 1234567890123,
  139. SingleFloat = 12.25f,
  140. SingleForeignEnum = ForeignEnum.ForeignBar,
  141. SingleForeignMessage = new ForeignMessage { C = 10 },
  142. SingleImportEnum = ImportEnum.ImportBaz,
  143. SingleImportMessage = new ImportMessage { D = 20 },
  144. SingleInt32 = 100,
  145. SingleInt64 = 3210987654321,
  146. SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo,
  147. SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
  148. SinglePublicImportMessage = new PublicImportMessage { E = 54 },
  149. SingleSfixed32 = -123,
  150. SingleSfixed64 = -12345678901234,
  151. SingleSint32 = -456,
  152. SingleSint64 = -12345678901235,
  153. SingleString = "test",
  154. SingleUint32 = uint.MaxValue,
  155. SingleUint64 = ulong.MaxValue
  156. };
  157. byte[] bytes = message.ToByteArray();
  158. TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);
  159. Assert.AreEqual(message, parsed);
  160. }
  161. [Test]
  162. public void RoundTrip_RepeatedValues()
  163. {
  164. var message = new TestAllTypes
  165. {
  166. RepeatedBool = { true, false },
  167. RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6) },
  168. RepeatedDouble = { -12.25, 23.5 },
  169. RepeatedFixed32 = { uint.MaxValue, 23 },
  170. RepeatedFixed64 = { ulong.MaxValue, 1234567890123 },
  171. RepeatedFloat = { 100f, 12.25f },
  172. RepeatedForeignEnum = { ForeignEnum.ForeignFoo, ForeignEnum.ForeignBar },
  173. RepeatedForeignMessage = { new ForeignMessage(), new ForeignMessage { C = 10 } },
  174. RepeatedImportEnum = { ImportEnum.ImportBaz, ImportEnum.Unspecified },
  175. RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } },
  176. RepeatedInt32 = { 100, 200 },
  177. RepeatedInt64 = { 3210987654321, long.MaxValue },
  178. RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.Foo, TestAllTypes.Types.NestedEnum.Neg },
  179. RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } },
  180. RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } },
  181. RepeatedSfixed32 = { -123, 123 },
  182. RepeatedSfixed64 = { -12345678901234, 12345678901234 },
  183. RepeatedSint32 = { -456, 100 },
  184. RepeatedSint64 = { -12345678901235, 123 },
  185. RepeatedString = { "foo", "bar" },
  186. RepeatedUint32 = { uint.MaxValue, uint.MinValue },
  187. RepeatedUint64 = { ulong.MaxValue, uint.MinValue }
  188. };
  189. byte[] bytes = message.ToByteArray();
  190. TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);
  191. Assert.AreEqual(message, parsed);
  192. }
  193. // Note that not every map within map_unittest_proto3 is used. They all go through very
  194. // similar code paths. The fact that all maps are present is validation that we have codecs
  195. // for every type.
  196. [Test]
  197. public void RoundTrip_Maps()
  198. {
  199. var message = new TestMap
  200. {
  201. MapBoolBool = {
  202. { false, true },
  203. { true, false }
  204. },
  205. MapInt32Bytes = {
  206. { 5, ByteString.CopyFrom(6, 7, 8) },
  207. { 25, ByteString.CopyFrom(1, 2, 3, 4, 5) },
  208. { 10, ByteString.Empty }
  209. },
  210. MapInt32ForeignMessage = {
  211. { 0, new ForeignMessage { C = 10 } },
  212. { 5, new ForeignMessage() },
  213. },
  214. MapInt32Enum = {
  215. { 1, MapEnum.Bar },
  216. { 2000, MapEnum.Foo }
  217. }
  218. };
  219. byte[] bytes = message.ToByteArray();
  220. TestMap parsed = TestMap.Parser.ParseFrom(bytes);
  221. Assert.AreEqual(message, parsed);
  222. }
  223. [Test]
  224. public void MapWithEmptyEntry()
  225. {
  226. var message = new TestMap
  227. {
  228. MapInt32Bytes = { { 0, ByteString.Empty } }
  229. };
  230. byte[] bytes = message.ToByteArray();
  231. Assert.AreEqual(2, bytes.Length); // Tag for field entry (1 byte), length of entry (0; 1 byte)
  232. var parsed = TestMap.Parser.ParseFrom(bytes);
  233. Assert.AreEqual(1, parsed.MapInt32Bytes.Count);
  234. Assert.AreEqual(ByteString.Empty, parsed.MapInt32Bytes[0]);
  235. }
  236. [Test]
  237. public void MapWithOnlyValue()
  238. {
  239. // Hand-craft the stream to contain a single entry with just a value.
  240. var memoryStream = new MemoryStream();
  241. var output = new CodedOutputStream(memoryStream);
  242. output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited);
  243. var nestedMessage = new ForeignMessage { C = 20 };
  244. // Size of the entry (tag, size written by WriteMessage, data written by WriteMessage)
  245. output.WriteLength(2 + nestedMessage.CalculateSize());
  246. output.WriteTag(2, WireFormat.WireType.LengthDelimited);
  247. output.WriteMessage(nestedMessage);
  248. output.Flush();
  249. var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
  250. Assert.AreEqual(nestedMessage, parsed.MapInt32ForeignMessage[0]);
  251. }
  252. [Test]
  253. public void MapWithOnlyKey_PrimitiveValue()
  254. {
  255. // Hand-craft the stream to contain a single entry with just a key.
  256. var memoryStream = new MemoryStream();
  257. var output = new CodedOutputStream(memoryStream);
  258. output.WriteTag(TestMap.MapInt32DoubleFieldNumber, WireFormat.WireType.LengthDelimited);
  259. int key = 10;
  260. output.WriteLength(1 + CodedOutputStream.ComputeInt32Size(key));
  261. output.WriteTag(1, WireFormat.WireType.Varint);
  262. output.WriteInt32(key);
  263. output.Flush();
  264. var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
  265. Assert.AreEqual(0.0, parsed.MapInt32Double[key]);
  266. }
  267. [Test]
  268. public void MapWithOnlyKey_MessageValue()
  269. {
  270. // Hand-craft the stream to contain a single entry with just a key.
  271. var memoryStream = new MemoryStream();
  272. var output = new CodedOutputStream(memoryStream);
  273. output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited);
  274. int key = 10;
  275. output.WriteLength(1 + CodedOutputStream.ComputeInt32Size(key));
  276. output.WriteTag(1, WireFormat.WireType.Varint);
  277. output.WriteInt32(key);
  278. output.Flush();
  279. var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
  280. Assert.AreEqual(new ForeignMessage(), parsed.MapInt32ForeignMessage[key]);
  281. }
  282. [Test]
  283. public void MapIgnoresExtraFieldsWithinEntryMessages()
  284. {
  285. // Hand-craft the stream to contain a single entry with three fields
  286. var memoryStream = new MemoryStream();
  287. var output = new CodedOutputStream(memoryStream);
  288. output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
  289. var key = 10; // Field 1
  290. var value = 20; // Field 2
  291. var extra = 30; // Field 3
  292. // Each field can be represented in a single byte, with a single byte tag.
  293. // Total message size: 6 bytes.
  294. output.WriteLength(6);
  295. output.WriteTag(1, WireFormat.WireType.Varint);
  296. output.WriteInt32(key);
  297. output.WriteTag(2, WireFormat.WireType.Varint);
  298. output.WriteInt32(value);
  299. output.WriteTag(3, WireFormat.WireType.Varint);
  300. output.WriteInt32(extra);
  301. output.Flush();
  302. var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
  303. Assert.AreEqual(value, parsed.MapInt32Int32[key]);
  304. }
  305. [Test]
  306. public void MapFieldOrderIsIrrelevant()
  307. {
  308. var memoryStream = new MemoryStream();
  309. var output = new CodedOutputStream(memoryStream);
  310. output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
  311. var key = 10;
  312. var value = 20;
  313. // Each field can be represented in a single byte, with a single byte tag.
  314. // Total message size: 4 bytes.
  315. output.WriteLength(4);
  316. output.WriteTag(2, WireFormat.WireType.Varint);
  317. output.WriteInt32(value);
  318. output.WriteTag(1, WireFormat.WireType.Varint);
  319. output.WriteInt32(key);
  320. output.Flush();
  321. var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
  322. Assert.AreEqual(value, parsed.MapInt32Int32[key]);
  323. }
  324. [Test]
  325. public void MapNonContiguousEntries()
  326. {
  327. var memoryStream = new MemoryStream();
  328. var output = new CodedOutputStream(memoryStream);
  329. // Message structure:
  330. // Entry for MapInt32Int32
  331. // Entry for MapStringString
  332. // Entry for MapInt32Int32
  333. // First entry
  334. var key1 = 10;
  335. var value1 = 20;
  336. output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
  337. output.WriteLength(4);
  338. output.WriteTag(1, WireFormat.WireType.Varint);
  339. output.WriteInt32(key1);
  340. output.WriteTag(2, WireFormat.WireType.Varint);
  341. output.WriteInt32(value1);
  342. // Second entry
  343. var key2 = "a";
  344. var value2 = "b";
  345. output.WriteTag(TestMap.MapStringStringFieldNumber, WireFormat.WireType.LengthDelimited);
  346. output.WriteLength(6); // 3 bytes per entry: tag, size, character
  347. output.WriteTag(1, WireFormat.WireType.LengthDelimited);
  348. output.WriteString(key2);
  349. output.WriteTag(2, WireFormat.WireType.LengthDelimited);
  350. output.WriteString(value2);
  351. // Third entry
  352. var key3 = 15;
  353. var value3 = 25;
  354. output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
  355. output.WriteLength(4);
  356. output.WriteTag(1, WireFormat.WireType.Varint);
  357. output.WriteInt32(key3);
  358. output.WriteTag(2, WireFormat.WireType.Varint);
  359. output.WriteInt32(value3);
  360. output.Flush();
  361. var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
  362. var expected = new TestMap
  363. {
  364. MapInt32Int32 = { { key1, value1 }, { key3, value3 } },
  365. MapStringString = { { key2, value2 } }
  366. };
  367. Assert.AreEqual(expected, parsed);
  368. }
  369. [Test]
  370. public void DuplicateKeys_LastEntryWins()
  371. {
  372. var memoryStream = new MemoryStream();
  373. var output = new CodedOutputStream(memoryStream);
  374. var key = 10;
  375. var value1 = 20;
  376. var value2 = 30;
  377. // First entry
  378. output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
  379. output.WriteLength(4);
  380. output.WriteTag(1, WireFormat.WireType.Varint);
  381. output.WriteInt32(key);
  382. output.WriteTag(2, WireFormat.WireType.Varint);
  383. output.WriteInt32(value1);
  384. // Second entry - same key, different value
  385. output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
  386. output.WriteLength(4);
  387. output.WriteTag(1, WireFormat.WireType.Varint);
  388. output.WriteInt32(key);
  389. output.WriteTag(2, WireFormat.WireType.Varint);
  390. output.WriteInt32(value2);
  391. output.Flush();
  392. var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
  393. Assert.AreEqual(value2, parsed.MapInt32Int32[key]);
  394. }
  395. [Test]
  396. public void CloneSingleNonMessageValues()
  397. {
  398. var original = new TestAllTypes
  399. {
  400. SingleBool = true,
  401. SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
  402. SingleDouble = 23.5,
  403. SingleFixed32 = 23,
  404. SingleFixed64 = 1234567890123,
  405. SingleFloat = 12.25f,
  406. SingleInt32 = 100,
  407. SingleInt64 = 3210987654321,
  408. SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo,
  409. SingleSfixed32 = -123,
  410. SingleSfixed64 = -12345678901234,
  411. SingleSint32 = -456,
  412. SingleSint64 = -12345678901235,
  413. SingleString = "test",
  414. SingleUint32 = uint.MaxValue,
  415. SingleUint64 = ulong.MaxValue
  416. };
  417. var clone = original.Clone();
  418. Assert.AreNotSame(original, clone);
  419. Assert.AreEqual(original, clone);
  420. // Just as a single example
  421. clone.SingleInt32 = 150;
  422. Assert.AreNotEqual(original, clone);
  423. }
  424. [Test]
  425. public void CloneRepeatedNonMessageValues()
  426. {
  427. var original = new TestAllTypes
  428. {
  429. RepeatedBool = { true, false },
  430. RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6) },
  431. RepeatedDouble = { -12.25, 23.5 },
  432. RepeatedFixed32 = { uint.MaxValue, 23 },
  433. RepeatedFixed64 = { ulong.MaxValue, 1234567890123 },
  434. RepeatedFloat = { 100f, 12.25f },
  435. RepeatedInt32 = { 100, 200 },
  436. RepeatedInt64 = { 3210987654321, long.MaxValue },
  437. RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.Foo, TestAllTypes.Types.NestedEnum.Neg },
  438. RepeatedSfixed32 = { -123, 123 },
  439. RepeatedSfixed64 = { -12345678901234, 12345678901234 },
  440. RepeatedSint32 = { -456, 100 },
  441. RepeatedSint64 = { -12345678901235, 123 },
  442. RepeatedString = { "foo", "bar" },
  443. RepeatedUint32 = { uint.MaxValue, uint.MinValue },
  444. RepeatedUint64 = { ulong.MaxValue, uint.MinValue }
  445. };
  446. var clone = original.Clone();
  447. Assert.AreNotSame(original, clone);
  448. Assert.AreEqual(original, clone);
  449. // Just as a single example
  450. clone.RepeatedDouble.Add(25.5);
  451. Assert.AreNotEqual(original, clone);
  452. }
  453. [Test]
  454. public void CloneSingleMessageField()
  455. {
  456. var original = new TestAllTypes
  457. {
  458. SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 }
  459. };
  460. var clone = original.Clone();
  461. Assert.AreNotSame(original, clone);
  462. Assert.AreNotSame(original.SingleNestedMessage, clone.SingleNestedMessage);
  463. Assert.AreEqual(original, clone);
  464. clone.SingleNestedMessage.Bb = 30;
  465. Assert.AreNotEqual(original, clone);
  466. }
  467. [Test]
  468. public void CloneRepeatedMessageField()
  469. {
  470. var original = new TestAllTypes
  471. {
  472. RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 20 } }
  473. };
  474. var clone = original.Clone();
  475. Assert.AreNotSame(original, clone);
  476. Assert.AreNotSame(original.RepeatedNestedMessage, clone.RepeatedNestedMessage);
  477. Assert.AreNotSame(original.RepeatedNestedMessage[0], clone.RepeatedNestedMessage[0]);
  478. Assert.AreEqual(original, clone);
  479. clone.RepeatedNestedMessage[0].Bb = 30;
  480. Assert.AreNotEqual(original, clone);
  481. }
  482. [Test]
  483. public void CloneOneofField()
  484. {
  485. var original = new TestAllTypes
  486. {
  487. OneofNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 }
  488. };
  489. var clone = original.Clone();
  490. Assert.AreNotSame(original, clone);
  491. Assert.AreEqual(original, clone);
  492. // We should have cloned the message
  493. original.OneofNestedMessage.Bb = 30;
  494. Assert.AreNotEqual(original, clone);
  495. }
  496. [Test]
  497. public void OneofProperties()
  498. {
  499. // Switch the oneof case between each of the different options, and check everything behaves
  500. // as expected in each case.
  501. var message = new TestAllTypes();
  502. Assert.AreEqual("", message.OneofString);
  503. Assert.AreEqual(0, message.OneofUint32);
  504. Assert.AreEqual(ByteString.Empty, message.OneofBytes);
  505. Assert.IsNull(message.OneofNestedMessage);
  506. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
  507. message.OneofString = "sample";
  508. Assert.AreEqual("sample", message.OneofString);
  509. Assert.AreEqual(0, message.OneofUint32);
  510. Assert.AreEqual(ByteString.Empty, message.OneofBytes);
  511. Assert.IsNull(message.OneofNestedMessage);
  512. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofString, message.OneofFieldCase);
  513. var bytes = ByteString.CopyFrom(1, 2, 3);
  514. message.OneofBytes = bytes;
  515. Assert.AreEqual("", message.OneofString);
  516. Assert.AreEqual(0, message.OneofUint32);
  517. Assert.AreEqual(bytes, message.OneofBytes);
  518. Assert.IsNull(message.OneofNestedMessage);
  519. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofBytes, message.OneofFieldCase);
  520. message.OneofUint32 = 20;
  521. Assert.AreEqual("", message.OneofString);
  522. Assert.AreEqual(20, message.OneofUint32);
  523. Assert.AreEqual(ByteString.Empty, message.OneofBytes);
  524. Assert.IsNull(message.OneofNestedMessage);
  525. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message.OneofFieldCase);
  526. var nestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 25 };
  527. message.OneofNestedMessage = nestedMessage;
  528. Assert.AreEqual("", message.OneofString);
  529. Assert.AreEqual(0, message.OneofUint32);
  530. Assert.AreEqual(ByteString.Empty, message.OneofBytes);
  531. Assert.AreEqual(nestedMessage, message.OneofNestedMessage);
  532. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofNestedMessage, message.OneofFieldCase);
  533. message.ClearOneofField();
  534. Assert.AreEqual("", message.OneofString);
  535. Assert.AreEqual(0, message.OneofUint32);
  536. Assert.AreEqual(ByteString.Empty, message.OneofBytes);
  537. Assert.IsNull(message.OneofNestedMessage);
  538. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
  539. }
  540. [Test]
  541. public void Oneof_DefaultValuesNotEqual()
  542. {
  543. var message1 = new TestAllTypes { OneofString = "" };
  544. var message2 = new TestAllTypes { OneofUint32 = 0 };
  545. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofString, message1.OneofFieldCase);
  546. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
  547. Assert.AreNotEqual(message1, message2);
  548. }
  549. [Test]
  550. public void OneofSerialization_NonDefaultValue()
  551. {
  552. var message = new TestAllTypes();
  553. message.OneofString = "this would take a bit of space";
  554. message.OneofUint32 = 10;
  555. var bytes = message.ToByteArray();
  556. Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - no string!
  557. var message2 = TestAllTypes.Parser.ParseFrom(bytes);
  558. Assert.AreEqual(message, message2);
  559. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
  560. }
  561. [Test]
  562. public void OneofSerialization_DefaultValue()
  563. {
  564. var message = new TestAllTypes();
  565. message.OneofString = "this would take a bit of space";
  566. message.OneofUint32 = 0; // This is the default value for UInt32; normally wouldn't be serialized
  567. var bytes = message.ToByteArray();
  568. Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - it's still serialized
  569. var message2 = TestAllTypes.Parser.ParseFrom(bytes);
  570. Assert.AreEqual(message, message2);
  571. Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
  572. }
  573. [Test]
  574. public void DiscardUnknownFields_RealDataStillRead()
  575. {
  576. var message = SampleMessages.CreateFullTestAllTypes();
  577. var stream = new MemoryStream();
  578. var output = new CodedOutputStream(stream);
  579. var unusedFieldNumber = 23456;
  580. Assert.IsFalse(TestAllTypes.Descriptor.Fields.InDeclarationOrder().Select(x => x.FieldNumber).Contains(unusedFieldNumber));
  581. output.WriteTag(unusedFieldNumber, WireFormat.WireType.LengthDelimited);
  582. output.WriteString("ignore me");
  583. message.WriteTo(output);
  584. output.Flush();
  585. stream.Position = 0;
  586. var parsed = TestAllTypes.Parser.ParseFrom(stream);
  587. // TODO(jieluo): Add test back after DiscardUnknownFields is supported
  588. // Assert.AreEqual(message, parsed);
  589. }
  590. [Test]
  591. public void DiscardUnknownFields_AllTypes()
  592. {
  593. // Simple way of ensuring we can skip all kinds of fields.
  594. var data = SampleMessages.CreateFullTestAllTypes().ToByteArray();
  595. var empty = Empty.Parser.ParseFrom(data);
  596. // TODO(jieluo): Add test back after DiscardUnknownField is supported.
  597. // Assert.AreEqual(new Empty(), empty);
  598. }
  599. // This was originally seen as a conformance test failure.
  600. [Test]
  601. public void TruncatedMessageFieldThrows()
  602. {
  603. // 130, 3 is the message tag
  604. // 1 is the data length - but there's no data.
  605. var data = new byte[] { 130, 3, 1 };
  606. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseFrom(data));
  607. }
  608. /// <summary>
  609. /// Demonstrates current behaviour with an extraneous end group tag - see issue 688
  610. /// for details; we may want to change this.
  611. /// </summary>
  612. [Test]
  613. public void ExtraEndGroupThrows()
  614. {
  615. var message = SampleMessages.CreateFullTestAllTypes();
  616. var stream = new MemoryStream();
  617. var output = new CodedOutputStream(stream);
  618. output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.WireType.Fixed32);
  619. output.WriteFixed32(123);
  620. output.WriteTag(100, WireFormat.WireType.EndGroup);
  621. output.Flush();
  622. stream.Position = 0;
  623. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseFrom(stream));
  624. }
  625. [Test]
  626. public void CustomDiagnosticMessage_DirectToStringCall()
  627. {
  628. var message = new ForeignMessage { C = 31 };
  629. Assert.AreEqual("{ \"c\": 31, \"@cInHex\": \"1f\" }", message.ToString());
  630. Assert.AreEqual("{ \"c\": 31 }", JsonFormatter.Default.Format(message));
  631. }
  632. [Test]
  633. public void CustomDiagnosticMessage_Nested()
  634. {
  635. var message = new TestAllTypes { SingleForeignMessage = new ForeignMessage { C = 16 } };
  636. Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16, \"@cInHex\": \"10\" } }", message.ToString());
  637. Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16 } }", JsonFormatter.Default.Format(message));
  638. }
  639. [Test]
  640. public void CustomDiagnosticMessage_DirectToTextWriterCall()
  641. {
  642. var message = new ForeignMessage { C = 31 };
  643. var writer = new StringWriter();
  644. JsonFormatter.Default.Format(message, writer);
  645. Assert.AreEqual("{ \"c\": 31 }", writer.ToString());
  646. }
  647. }
  648. }