pbliteserializer_test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.proto2.PbLiteSerializerTest');
  15. goog.setTestOnly('goog.proto2.PbLiteSerializerTest');
  16. goog.require('goog.proto2.PbLiteSerializer');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('proto2.TestAllTypes');
  19. function testSerializationAndDeserialization() {
  20. var message = createPopulatedMessage();
  21. // Serialize.
  22. var serializer = new goog.proto2.PbLiteSerializer();
  23. var pblite = serializer.serialize(message);
  24. assertTrue(goog.isArray(pblite));
  25. // Assert that everything serialized properly.
  26. assertEquals(101, pblite[1]);
  27. assertEquals('102', pblite[2]);
  28. assertEquals(103, pblite[3]);
  29. assertEquals('104', pblite[4]);
  30. assertEquals(105, pblite[5]);
  31. assertEquals('106', pblite[6]);
  32. assertEquals(107, pblite[7]);
  33. assertEquals('108', pblite[8]);
  34. assertEquals(109, pblite[9]);
  35. assertEquals('110', pblite[10]);
  36. assertEquals(111.5, pblite[11]);
  37. assertEquals(112.5, pblite[12]);
  38. assertEquals(1, pblite[13]); // true is serialized as 1
  39. assertEquals('test', pblite[14]);
  40. assertEquals('abcd', pblite[15]);
  41. assertEquals(111, pblite[16][17]);
  42. assertEquals(112, pblite[18][1]);
  43. assertTrue(pblite[19] === undefined);
  44. assertTrue(pblite[20] === undefined);
  45. assertEquals(proto2.TestAllTypes.NestedEnum.FOO, pblite[21]);
  46. assertEquals(201, pblite[31][0]);
  47. assertEquals(202, pblite[31][1]);
  48. assertEquals('foo', pblite[44][0]);
  49. assertEquals('bar', pblite[44][1]);
  50. var serializer = new goog.proto2.PbLiteSerializer();
  51. // Deserialize.
  52. var messageCopy =
  53. serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
  54. assertNotEquals(messageCopy, message);
  55. assertDeserializationMatches(messageCopy);
  56. }
  57. function testZeroBasedSerializationAndDeserialization() {
  58. var message = createPopulatedMessage();
  59. // Serialize.
  60. var serializer = new goog.proto2.PbLiteSerializer();
  61. serializer.setZeroIndexed(true);
  62. var pblite = serializer.serialize(message);
  63. assertTrue(goog.isArray(pblite));
  64. // Assert that everything serialized properly.
  65. assertEquals(101, pblite[0]);
  66. assertEquals('102', pblite[1]);
  67. assertEquals(103, pblite[2]);
  68. assertEquals('104', pblite[3]);
  69. assertEquals(105, pblite[4]);
  70. assertEquals('106', pblite[5]);
  71. assertEquals(107, pblite[6]);
  72. assertEquals('108', pblite[7]);
  73. assertEquals(109, pblite[8]);
  74. assertEquals('110', pblite[9]);
  75. assertEquals(111.5, pblite[10]);
  76. assertEquals(112.5, pblite[11]);
  77. assertEquals(1, pblite[12]); // true is serialized as 1
  78. assertEquals('test', pblite[13]);
  79. assertEquals('abcd', pblite[14]);
  80. assertEquals(111, pblite[15][16]);
  81. assertEquals(112, pblite[17][0]);
  82. assertTrue(pblite[18] === undefined);
  83. assertTrue(pblite[19] === undefined);
  84. assertEquals(proto2.TestAllTypes.NestedEnum.FOO, pblite[20]);
  85. assertEquals(201, pblite[30][0]);
  86. assertEquals(202, pblite[30][1]);
  87. assertEquals('foo', pblite[43][0]);
  88. assertEquals('bar', pblite[43][1]);
  89. // Deserialize.
  90. var messageCopy =
  91. serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
  92. assertNotEquals(messageCopy, message);
  93. assertEquals(message.getOptionalInt32(), messageCopy.getOptionalInt32());
  94. assertDeserializationMatches(messageCopy);
  95. }
  96. function createPopulatedMessage() {
  97. var message = new proto2.TestAllTypes();
  98. // Set the fields.
  99. // Singular.
  100. message.setOptionalInt32(101);
  101. message.setOptionalInt64('102');
  102. message.setOptionalUint32(103);
  103. message.setOptionalUint64('104');
  104. message.setOptionalSint32(105);
  105. message.setOptionalSint64('106');
  106. message.setOptionalFixed32(107);
  107. message.setOptionalFixed64('108');
  108. message.setOptionalSfixed32(109);
  109. message.setOptionalSfixed64('110');
  110. message.setOptionalFloat(111.5);
  111. message.setOptionalDouble(112.5);
  112. message.setOptionalBool(true);
  113. message.setOptionalString('test');
  114. message.setOptionalBytes('abcd');
  115. var group = new proto2.TestAllTypes.OptionalGroup();
  116. group.setA(111);
  117. message.setOptionalgroup(group);
  118. var nestedMessage = new proto2.TestAllTypes.NestedMessage();
  119. nestedMessage.setB(112);
  120. message.setOptionalNestedMessage(nestedMessage);
  121. message.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
  122. // Repeated.
  123. message.addRepeatedInt32(201);
  124. message.addRepeatedInt32(202);
  125. // Skip a few repeated fields so we can test how null array values are
  126. // handled.
  127. message.addRepeatedString('foo');
  128. message.addRepeatedString('bar');
  129. return message;
  130. }
  131. function testDeserializationFromExternalSource() {
  132. // Test deserialization where the JSON array is initialized from something
  133. // outside the Closure proto2 library, such as the JsPbLite library, or
  134. // manually as in this test.
  135. var pblite = [
  136. , // 0
  137. 101, // 1
  138. '102', // 2
  139. 103, // 3
  140. '104', // 4
  141. 105, // 5
  142. '106', // 6
  143. 107, // 7
  144. '108', // 8
  145. 109, // 9
  146. '110', // 10
  147. 111.5, // 11
  148. 112.5, // 12
  149. 1, // 13
  150. 'test', // 14
  151. 'abcd', // 15
  152. [
  153. , , , , , , , , , , , , , , , , , 111
  154. ], // 16, note the 17 commas so value is index 17
  155. , // 17
  156. [, 112], // 18
  157. ,
  158. , // 19-20
  159. proto2.TestAllTypes.NestedEnum.FOO, // 21
  160. , , , , , , , , , // 22-30
  161. [201, 202], // 31
  162. , , , , , , , , , , , , // 32-43
  163. ['foo', 'bar'] // 44
  164. ];
  165. pblite.length = 50;
  166. // Deserialize.
  167. var serializer = new goog.proto2.PbLiteSerializer();
  168. var messageCopy =
  169. serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
  170. assertDeserializationMatches(messageCopy);
  171. // http://b/issue?id=2928075
  172. assertFalse(messageCopy.hasRepeatedInt64());
  173. assertEquals(0, messageCopy.repeatedInt64Count());
  174. messageCopy.repeatedInt64Array();
  175. assertFalse(messageCopy.hasRepeatedInt64());
  176. assertEquals(0, messageCopy.repeatedInt64Count());
  177. // Access a nested message to ensure it is deserialized.
  178. assertNotNull(messageCopy.getOptionalNestedMessage());
  179. // Verify that the pblite array itself has not been replaced by the
  180. // deserialization.
  181. assertEquals('array', goog.typeOf(pblite[16]));
  182. // Update some fields and verify that the changes work with the lazy
  183. // deserializer.
  184. messageCopy.setOptionalBool(true);
  185. assertTrue(messageCopy.getOptionalBool());
  186. messageCopy.setOptionalBool(false);
  187. assertFalse(messageCopy.getOptionalBool());
  188. messageCopy.setOptionalInt32(1234);
  189. assertEquals(1234, messageCopy.getOptionalInt32());
  190. }
  191. function testModifyLazyDeserializedMessage() {
  192. var pblite = [
  193. , // 0
  194. 101, // 1
  195. '102', // 2
  196. 103, // 3
  197. '104', // 4
  198. 105, // 5
  199. '106', // 6
  200. 107, // 7
  201. '108', // 8
  202. 109, // 9
  203. '110', // 10
  204. 111.5, // 11
  205. 112.5, // 12
  206. 1, // 13
  207. 'test', // 14
  208. 'abcd', // 15
  209. [
  210. , , , , , , , , , , , , , , , , , 111
  211. ], // 16, note the 17 commas so value is index 17
  212. , // 17
  213. [, 112], // 18
  214. ,
  215. , // 19-20
  216. proto2.TestAllTypes.NestedEnum.FOO, // 21
  217. , , , , , , , , , // 22-30
  218. [201, 202], // 31
  219. , , , , , , , , , , , , // 32-43
  220. ['foo', 'bar'] // 44
  221. ];
  222. pblite.length = 50;
  223. // Deserialize.
  224. var serializer = new goog.proto2.PbLiteSerializer();
  225. var message =
  226. serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
  227. // Conduct some operations, ensuring that they all work as expected, even with
  228. // the lazily deserialized data.
  229. assertEquals(101, message.getOptionalInt32());
  230. message.setOptionalInt32(401);
  231. assertEquals(401, message.getOptionalInt32());
  232. assertEquals(2, message.repeatedInt32Count());
  233. assertEquals(201, message.getRepeatedInt32(0));
  234. assertEquals(202, message.getRepeatedInt32(1));
  235. message.clearRepeatedInt32();
  236. assertEquals(0, message.repeatedInt32Count());
  237. message.addRepeatedInt32(101);
  238. assertEquals(1, message.repeatedInt32Count());
  239. assertEquals(101, message.getRepeatedInt32(0));
  240. message.setUnknown(12345, 601);
  241. message.forEachUnknown(function(tag, value) {
  242. assertEquals(12345, tag);
  243. assertEquals(601, value);
  244. });
  245. // Create a copy of the message.
  246. var messageCopy = new proto2.TestAllTypes();
  247. messageCopy.copyFrom(message);
  248. assertEquals(1, messageCopy.repeatedInt32Count());
  249. assertEquals(101, messageCopy.getRepeatedInt32(0));
  250. }
  251. function testModifyLazyDeserializedMessageByAddingMessage() {
  252. var pblite = [
  253. , // 0
  254. 101, // 1
  255. '102', // 2
  256. 103, // 3
  257. '104', // 4
  258. 105, // 5
  259. '106', // 6
  260. 107, // 7
  261. '108', // 8
  262. 109, // 9
  263. '110', // 10
  264. 111.5, // 11
  265. 112.5, // 12
  266. 1, // 13
  267. 'test', // 14
  268. 'abcd', // 15
  269. [
  270. , , , , , , , , , , , , , , , , , 111
  271. ], // 16, note the 17 commas so value is index 17
  272. , // 17
  273. [, 112], // 18
  274. ,
  275. , // 19-20
  276. proto2.TestAllTypes.NestedEnum.FOO, // 21
  277. , , , , , , , , , // 22-30
  278. [201, 202], // 31
  279. , , , , , , , , , , , , // 32-43
  280. ['foo', 'bar'] // 44
  281. ];
  282. pblite.length = 50;
  283. // Deserialize.
  284. var serializer = new goog.proto2.PbLiteSerializer();
  285. var message =
  286. serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pblite);
  287. // Add a new nested message.
  288. var nested1 = new proto2.TestAllTypes.NestedMessage();
  289. nested1.setB(1234);
  290. var nested2 = new proto2.TestAllTypes.NestedMessage();
  291. nested2.setB(4567);
  292. message.addRepeatedNestedMessage(nested1);
  293. // Check the new nested message.
  294. assertEquals(1, message.repeatedNestedMessageArray().length);
  295. assertTrue(message.repeatedNestedMessageArray()[0].equals(nested1));
  296. // Add another nested message.
  297. message.addRepeatedNestedMessage(nested2);
  298. // Check both nested messages.
  299. assertEquals(2, message.repeatedNestedMessageArray().length);
  300. assertTrue(message.repeatedNestedMessageArray()[0].equals(nested1));
  301. assertTrue(message.repeatedNestedMessageArray()[1].equals(nested2));
  302. }
  303. function assertDeserializationMatches(messageCopy) {
  304. assertNotNull(messageCopy);
  305. assertTrue(messageCopy.hasOptionalInt32());
  306. assertTrue(messageCopy.hasOptionalInt64());
  307. assertTrue(messageCopy.hasOptionalUint32());
  308. assertTrue(messageCopy.hasOptionalUint64());
  309. assertTrue(messageCopy.hasOptionalSint32());
  310. assertTrue(messageCopy.hasOptionalSint64());
  311. assertTrue(messageCopy.hasOptionalFixed32());
  312. assertTrue(messageCopy.hasOptionalFixed64());
  313. assertTrue(messageCopy.hasOptionalSfixed32());
  314. assertTrue(messageCopy.hasOptionalSfixed64());
  315. assertTrue(messageCopy.hasOptionalFloat());
  316. assertTrue(messageCopy.hasOptionalDouble());
  317. assertTrue(messageCopy.hasOptionalBool());
  318. assertTrue(messageCopy.hasOptionalString());
  319. assertTrue(messageCopy.hasOptionalBytes());
  320. assertTrue(messageCopy.hasOptionalgroup());
  321. assertTrue(messageCopy.hasOptionalNestedMessage());
  322. assertTrue(messageCopy.hasOptionalNestedEnum());
  323. assertTrue(messageCopy.hasRepeatedInt32());
  324. assertFalse(messageCopy.hasRepeatedInt64());
  325. assertFalse(messageCopy.hasRepeatedUint32());
  326. assertFalse(messageCopy.hasRepeatedUint64());
  327. assertFalse(messageCopy.hasRepeatedSint32());
  328. assertFalse(messageCopy.hasRepeatedSint64());
  329. assertFalse(messageCopy.hasRepeatedFixed32());
  330. assertFalse(messageCopy.hasRepeatedFixed64());
  331. assertFalse(messageCopy.hasRepeatedSfixed32());
  332. assertFalse(messageCopy.hasRepeatedSfixed64());
  333. assertFalse(messageCopy.hasRepeatedFloat());
  334. assertFalse(messageCopy.hasRepeatedDouble());
  335. assertFalse(messageCopy.hasRepeatedBool());
  336. assertTrue(messageCopy.hasRepeatedString());
  337. assertFalse(messageCopy.hasRepeatedBytes());
  338. assertFalse(messageCopy.hasRepeatedgroup());
  339. assertFalse(messageCopy.hasRepeatedNestedMessage());
  340. assertFalse(messageCopy.hasRepeatedNestedEnum());
  341. assertEquals(1, messageCopy.optionalInt32Count());
  342. assertEquals(1, messageCopy.optionalInt64Count());
  343. assertEquals(1, messageCopy.optionalUint32Count());
  344. assertEquals(1, messageCopy.optionalUint64Count());
  345. assertEquals(1, messageCopy.optionalSint32Count());
  346. assertEquals(1, messageCopy.optionalSint64Count());
  347. assertEquals(1, messageCopy.optionalFixed32Count());
  348. assertEquals(1, messageCopy.optionalFixed64Count());
  349. assertEquals(1, messageCopy.optionalSfixed32Count());
  350. assertEquals(1, messageCopy.optionalSfixed64Count());
  351. assertEquals(1, messageCopy.optionalFloatCount());
  352. assertEquals(1, messageCopy.optionalDoubleCount());
  353. assertEquals(1, messageCopy.optionalBoolCount());
  354. assertEquals(1, messageCopy.optionalStringCount());
  355. assertEquals(1, messageCopy.optionalBytesCount());
  356. assertEquals(1, messageCopy.optionalgroupCount());
  357. assertEquals(1, messageCopy.optionalNestedMessageCount());
  358. assertEquals(1, messageCopy.optionalNestedEnumCount());
  359. assertEquals(2, messageCopy.repeatedInt32Count());
  360. assertEquals(0, messageCopy.repeatedInt64Count());
  361. assertEquals(0, messageCopy.repeatedUint32Count());
  362. assertEquals(0, messageCopy.repeatedUint64Count());
  363. assertEquals(0, messageCopy.repeatedSint32Count());
  364. assertEquals(0, messageCopy.repeatedSint64Count());
  365. assertEquals(0, messageCopy.repeatedFixed32Count());
  366. assertEquals(0, messageCopy.repeatedFixed64Count());
  367. assertEquals(0, messageCopy.repeatedSfixed32Count());
  368. assertEquals(0, messageCopy.repeatedSfixed64Count());
  369. assertEquals(0, messageCopy.repeatedFloatCount());
  370. assertEquals(0, messageCopy.repeatedDoubleCount());
  371. assertEquals(0, messageCopy.repeatedBoolCount());
  372. assertEquals(2, messageCopy.repeatedStringCount());
  373. assertEquals(0, messageCopy.repeatedBytesCount());
  374. assertEquals(0, messageCopy.repeatedgroupCount());
  375. assertEquals(0, messageCopy.repeatedNestedMessageCount());
  376. assertEquals(0, messageCopy.repeatedNestedEnumCount());
  377. assertEquals(101, messageCopy.getOptionalInt32());
  378. assertEquals('102', messageCopy.getOptionalInt64());
  379. assertEquals(103, messageCopy.getOptionalUint32());
  380. assertEquals('104', messageCopy.getOptionalUint64());
  381. assertEquals(105, messageCopy.getOptionalSint32());
  382. assertEquals('106', messageCopy.getOptionalSint64());
  383. assertEquals(107, messageCopy.getOptionalFixed32());
  384. assertEquals('108', messageCopy.getOptionalFixed64());
  385. assertEquals(109, messageCopy.getOptionalSfixed32());
  386. assertEquals('110', messageCopy.getOptionalSfixed64());
  387. assertEquals(111.5, messageCopy.getOptionalFloat());
  388. assertEquals(112.5, messageCopy.getOptionalDouble());
  389. assertEquals(true, messageCopy.getOptionalBool());
  390. assertEquals('test', messageCopy.getOptionalString());
  391. assertEquals('abcd', messageCopy.getOptionalBytes());
  392. assertEquals(111, messageCopy.getOptionalgroup().getA());
  393. assertEquals(112, messageCopy.getOptionalNestedMessage().getB());
  394. assertEquals(
  395. proto2.TestAllTypes.NestedEnum.FOO, messageCopy.getOptionalNestedEnum());
  396. assertEquals(201, messageCopy.getRepeatedInt32(0));
  397. assertEquals(202, messageCopy.getRepeatedInt32(1));
  398. }
  399. function testMergeFromLazyTarget() {
  400. var serializer = new goog.proto2.PbLiteSerializer();
  401. var source = new proto2.TestAllTypes();
  402. var nested = new proto2.TestAllTypes.NestedMessage();
  403. nested.setB(66);
  404. source.setOptionalNestedMessage(nested);
  405. source.setOptionalInt32(32);
  406. source.setOptionalString('foo');
  407. source.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
  408. source.addRepeatedInt32(2);
  409. var target = new proto2.TestAllTypes();
  410. nested = new proto2.TestAllTypes.NestedMessage();
  411. nested.setC(77);
  412. target.setOptionalNestedMessage(nested);
  413. target.setOptionalInt64('64');
  414. target.setOptionalString('bar');
  415. target.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.BAR);
  416. target.addRepeatedInt32(1);
  417. var pbliteTarget = serializer.serialize(target);
  418. var lazyTarget =
  419. serializer.deserialize(proto2.TestAllTypes.getDescriptor(), pbliteTarget);
  420. var expected = new proto2.TestAllTypes();
  421. nested = new proto2.TestAllTypes.NestedMessage();
  422. nested.setB(66);
  423. nested.setC(77);
  424. expected.setOptionalNestedMessage(nested);
  425. expected.setOptionalInt32(32);
  426. expected.setOptionalInt64('64');
  427. expected.setOptionalString('foo');
  428. expected.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
  429. expected.addRepeatedInt32(1);
  430. expected.addRepeatedInt32(2);
  431. lazyTarget.mergeFrom(source);
  432. assertTrue(
  433. 'expected and lazyTarget are equal after mergeFrom',
  434. lazyTarget.equals(expected));
  435. }