proto_test.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Test suite is written using Jasmine -- see http://jasmine.github.io/
  31. goog.require('goog.crypt.base64');
  32. goog.require('goog.testing.asserts');
  33. goog.require('jspb.BinaryWriter');
  34. goog.require('jspb.Message');
  35. // CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test
  36. goog.require('proto.jspb.test.ExtendsWithMessage');
  37. goog.require('proto.jspb.test.ForeignEnum');
  38. goog.require('proto.jspb.test.ForeignMessage');
  39. goog.require('proto.jspb.test.TestAllTypes');
  40. goog.require('proto.jspb.test.TestExtendable');
  41. goog.require('proto.jspb.test.extendOptionalBool');
  42. goog.require('proto.jspb.test.extendOptionalBytes');
  43. goog.require('proto.jspb.test.extendOptionalDouble');
  44. goog.require('proto.jspb.test.extendOptionalFixed32');
  45. goog.require('proto.jspb.test.extendOptionalFixed64');
  46. goog.require('proto.jspb.test.extendOptionalFloat');
  47. goog.require('proto.jspb.test.extendOptionalForeignEnum');
  48. goog.require('proto.jspb.test.extendOptionalInt32');
  49. goog.require('proto.jspb.test.extendOptionalInt64');
  50. goog.require('proto.jspb.test.extendOptionalSfixed32');
  51. goog.require('proto.jspb.test.extendOptionalSfixed64');
  52. goog.require('proto.jspb.test.extendOptionalSint32');
  53. goog.require('proto.jspb.test.extendOptionalSint64');
  54. goog.require('proto.jspb.test.extendOptionalString');
  55. goog.require('proto.jspb.test.extendOptionalUint32');
  56. goog.require('proto.jspb.test.extendOptionalUint64');
  57. goog.require('proto.jspb.test.extendPackedRepeatedBoolList');
  58. goog.require('proto.jspb.test.extendPackedRepeatedDoubleList');
  59. goog.require('proto.jspb.test.extendPackedRepeatedFixed32List');
  60. goog.require('proto.jspb.test.extendPackedRepeatedFixed64List');
  61. goog.require('proto.jspb.test.extendPackedRepeatedFloatList');
  62. goog.require('proto.jspb.test.extendPackedRepeatedForeignEnumList');
  63. goog.require('proto.jspb.test.extendPackedRepeatedInt32List');
  64. goog.require('proto.jspb.test.extendPackedRepeatedInt64List');
  65. goog.require('proto.jspb.test.extendPackedRepeatedSfixed32List');
  66. goog.require('proto.jspb.test.extendPackedRepeatedSfixed64List');
  67. goog.require('proto.jspb.test.extendPackedRepeatedSint32List');
  68. goog.require('proto.jspb.test.extendPackedRepeatedSint64List');
  69. goog.require('proto.jspb.test.extendPackedRepeatedUint32List');
  70. goog.require('proto.jspb.test.extendPackedRepeatedUint64List');
  71. goog.require('proto.jspb.test.extendRepeatedBoolList');
  72. goog.require('proto.jspb.test.extendRepeatedBytesList');
  73. goog.require('proto.jspb.test.extendRepeatedDoubleList');
  74. goog.require('proto.jspb.test.extendRepeatedFixed32List');
  75. goog.require('proto.jspb.test.extendRepeatedFixed64List');
  76. goog.require('proto.jspb.test.extendRepeatedFloatList');
  77. goog.require('proto.jspb.test.extendRepeatedForeignEnumList');
  78. goog.require('proto.jspb.test.extendRepeatedInt32List');
  79. goog.require('proto.jspb.test.extendRepeatedInt64List');
  80. goog.require('proto.jspb.test.extendRepeatedSfixed32List');
  81. goog.require('proto.jspb.test.extendRepeatedSfixed64List');
  82. goog.require('proto.jspb.test.extendRepeatedSint32List');
  83. goog.require('proto.jspb.test.extendRepeatedSint64List');
  84. goog.require('proto.jspb.test.extendRepeatedStringList');
  85. goog.require('proto.jspb.test.extendRepeatedUint32List');
  86. goog.require('proto.jspb.test.extendRepeatedUint64List');
  87. // CommonJS-LoadFromFile: ../node_modules/google-protobuf/google/protobuf/any_pb proto.google.protobuf
  88. goog.require('proto.google.protobuf.Any');
  89. var suite = {};
  90. var BYTES = new Uint8Array([1, 2, 8, 9]);
  91. var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES);
  92. /**
  93. * Helper: fill all fields on a TestAllTypes message.
  94. * @param {proto.jspb.test.TestAllTypes} msg
  95. */
  96. function fillAllFields(msg) {
  97. msg.setOptionalInt32(-42);
  98. // can be exactly represented by JS number (64-bit double, i.e., 52-bit
  99. // mantissa).
  100. msg.setOptionalInt64(-0x7fffffff00000000);
  101. msg.setOptionalUint32(0x80000000);
  102. msg.setOptionalUint64(0xf000000000000000);
  103. msg.setOptionalSint32(-100);
  104. msg.setOptionalSint64(-0x8000000000000000);
  105. msg.setOptionalFixed32(1234);
  106. msg.setOptionalFixed64(0x1234567800000000);
  107. msg.setOptionalSfixed32(-1234);
  108. msg.setOptionalSfixed64(-0x1234567800000000);
  109. msg.setOptionalFloat(1.5);
  110. msg.setOptionalDouble(-1.5);
  111. msg.setOptionalBool(true);
  112. msg.setOptionalString('hello world');
  113. msg.setOptionalBytes(BYTES);
  114. msg.setOptionalGroup(new proto.jspb.test.TestAllTypes.OptionalGroup());
  115. msg.getOptionalGroup().setA(100);
  116. var submsg = new proto.jspb.test.ForeignMessage();
  117. submsg.setC(16);
  118. msg.setOptionalForeignMessage(submsg);
  119. msg.setOptionalForeignEnum(proto.jspb.test.ForeignEnum.FOREIGN_FOO);
  120. msg.setOneofString('oneof');
  121. msg.setRepeatedInt32List([-42]);
  122. msg.setRepeatedInt64List([-0x7fffffff00000000]);
  123. msg.setRepeatedUint32List([0x80000000]);
  124. msg.setRepeatedUint64List([0xf000000000000000]);
  125. msg.setRepeatedSint32List([-100]);
  126. msg.setRepeatedSint64List([-0x8000000000000000]);
  127. msg.setRepeatedFixed32List([1234]);
  128. msg.setRepeatedFixed64List([0x1234567800000000]);
  129. msg.setRepeatedSfixed32List([-1234]);
  130. msg.setRepeatedSfixed64List([-0x1234567800000000]);
  131. msg.setRepeatedFloatList([1.5]);
  132. msg.setRepeatedDoubleList([-1.5]);
  133. msg.setRepeatedBoolList([true]);
  134. msg.setRepeatedStringList(['hello world']);
  135. msg.setRepeatedBytesList([BYTES, BYTES]);
  136. msg.setRepeatedGroupList([new proto.jspb.test.TestAllTypes.RepeatedGroup()]);
  137. msg.getRepeatedGroupList()[0].setA(100);
  138. submsg = new proto.jspb.test.ForeignMessage();
  139. submsg.setC(1000);
  140. msg.setRepeatedForeignMessageList([submsg]);
  141. msg.setRepeatedForeignEnumList([proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
  142. msg.setPackedRepeatedInt32List([-42]);
  143. msg.setPackedRepeatedInt64List([-0x7fffffff00000000]);
  144. msg.setPackedRepeatedUint32List([0x80000000]);
  145. msg.setPackedRepeatedUint64List([0xf000000000000000]);
  146. msg.setPackedRepeatedSint32List([-100]);
  147. msg.setPackedRepeatedSint64List([-0x8000000000000000]);
  148. msg.setPackedRepeatedFixed32List([1234]);
  149. msg.setPackedRepeatedFixed64List([0x1234567800000000]);
  150. msg.setPackedRepeatedSfixed32List([-1234]);
  151. msg.setPackedRepeatedSfixed64List([-0x1234567800000000]);
  152. msg.setPackedRepeatedFloatList([1.5]);
  153. msg.setPackedRepeatedDoubleList([-1.5]);
  154. msg.setPackedRepeatedBoolList([true]);
  155. }
  156. /**
  157. * Helper: compare a bytes field to an expected value
  158. * @param {Uint8Array|string} arr
  159. * @param {Uint8Array} expected
  160. * @return {boolean}
  161. */
  162. function bytesCompare(arr, expected) {
  163. if (typeof arr === 'string') {
  164. arr = goog.crypt.base64.decodeStringToUint8Array(arr);
  165. }
  166. if (arr.length != expected.length) {
  167. return false;
  168. }
  169. for (var i = 0; i < arr.length; i++) {
  170. if (arr[i] != expected[i]) {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. /**
  177. * Helper: verify contents of given TestAllTypes message as set by
  178. * fillAllFields().
  179. * @param {proto.jspb.test.TestAllTypes} original
  180. * @param {proto.jspb.test.TestAllTypes} copy
  181. */
  182. function checkAllFields(original, copy) {
  183. assertEquals(copy.getOptionalInt32(), -42);
  184. assertEquals(copy.getOptionalInt64(), -0x7fffffff00000000);
  185. assertEquals(copy.getOptionalUint32(), 0x80000000);
  186. assertEquals(copy.getOptionalUint64(), 0xf000000000000000);
  187. assertEquals(copy.getOptionalSint32(), -100);
  188. assertEquals(copy.getOptionalSint64(), -0x8000000000000000);
  189. assertEquals(copy.getOptionalFixed32(), 1234);
  190. assertEquals(copy.getOptionalFixed64(), 0x1234567800000000);
  191. assertEquals(copy.getOptionalSfixed32(), -1234);
  192. assertEquals(copy.getOptionalSfixed64(), -0x1234567800000000);
  193. assertEquals(copy.getOptionalFloat(), 1.5);
  194. assertEquals(copy.getOptionalDouble(), -1.5);
  195. assertEquals(copy.getOptionalBool(), true);
  196. assertEquals(copy.getOptionalString(), 'hello world');
  197. assertEquals(true, bytesCompare(copy.getOptionalBytes(), BYTES));
  198. assertEquals(true, bytesCompare(copy.getOptionalBytes_asU8(), BYTES));
  199. assertEquals(
  200. copy.getOptionalBytes_asB64(), goog.crypt.base64.encodeByteArray(BYTES));
  201. assertEquals(copy.getOptionalGroup().getA(), 100);
  202. assertEquals(copy.getOptionalForeignMessage().getC(), 16);
  203. assertEquals(copy.getOptionalForeignEnum(),
  204. proto.jspb.test.ForeignEnum.FOREIGN_FOO);
  205. assertEquals(copy.getOneofString(), 'oneof');
  206. assertEquals(copy.getOneofFieldCase(),
  207. proto.jspb.test.TestAllTypes.OneofFieldCase.ONEOF_STRING);
  208. assertElementsEquals(copy.getRepeatedInt32List(), [-42]);
  209. assertElementsEquals(copy.getRepeatedInt64List(), [-0x7fffffff00000000]);
  210. assertElementsEquals(copy.getRepeatedUint32List(), [0x80000000]);
  211. assertElementsEquals(copy.getRepeatedUint64List(), [0xf000000000000000]);
  212. assertElementsEquals(copy.getRepeatedSint32List(), [-100]);
  213. assertElementsEquals(copy.getRepeatedSint64List(), [-0x8000000000000000]);
  214. assertElementsEquals(copy.getRepeatedFixed32List(), [1234]);
  215. assertElementsEquals(copy.getRepeatedFixed64List(), [0x1234567800000000]);
  216. assertElementsEquals(copy.getRepeatedSfixed32List(), [-1234]);
  217. assertElementsEquals(copy.getRepeatedSfixed64List(), [-0x1234567800000000]);
  218. assertElementsEquals(copy.getRepeatedFloatList(), [1.5]);
  219. assertElementsEquals(copy.getRepeatedDoubleList(), [-1.5]);
  220. assertElementsEquals(copy.getRepeatedBoolList(), [true]);
  221. assertElementsEquals(copy.getRepeatedStringList(), ['hello world']);
  222. assertEquals(copy.getRepeatedBytesList().length, 2);
  223. assertEquals(true, bytesCompare(copy.getRepeatedBytesList_asU8()[0], BYTES));
  224. assertEquals(true, bytesCompare(copy.getRepeatedBytesList()[0], BYTES));
  225. assertEquals(true, bytesCompare(copy.getRepeatedBytesList_asU8()[1], BYTES));
  226. assertEquals(copy.getRepeatedBytesList_asB64()[0], BYTES_B64);
  227. assertEquals(copy.getRepeatedBytesList_asB64()[1], BYTES_B64);
  228. assertEquals(copy.getRepeatedGroupList().length, 1);
  229. assertEquals(copy.getRepeatedGroupList()[0].getA(), 100);
  230. assertEquals(copy.getRepeatedForeignMessageList().length, 1);
  231. assertEquals(copy.getRepeatedForeignMessageList()[0].getC(), 1000);
  232. assertElementsEquals(copy.getRepeatedForeignEnumList(),
  233. [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
  234. assertElementsEquals(copy.getPackedRepeatedInt32List(), [-42]);
  235. assertElementsEquals(copy.getPackedRepeatedInt64List(),
  236. [-0x7fffffff00000000]);
  237. assertElementsEquals(copy.getPackedRepeatedUint32List(), [0x80000000]);
  238. assertElementsEquals(copy.getPackedRepeatedUint64List(),
  239. [0xf000000000000000]);
  240. assertElementsEquals(copy.getPackedRepeatedSint32List(), [-100]);
  241. assertElementsEquals(copy.getPackedRepeatedSint64List(),
  242. [-0x8000000000000000]);
  243. assertElementsEquals(copy.getPackedRepeatedFixed32List(), [1234]);
  244. assertElementsEquals(copy.getPackedRepeatedFixed64List(),
  245. [0x1234567800000000]);
  246. assertElementsEquals(copy.getPackedRepeatedSfixed32List(), [-1234]);
  247. assertElementsEquals(copy.getPackedRepeatedSfixed64List(),
  248. [-0x1234567800000000]);
  249. assertElementsEquals(copy.getPackedRepeatedFloatList(), [1.5]);
  250. assertElementsEquals(copy.getPackedRepeatedDoubleList(), [-1.5]);
  251. // Check last so we get more granular errors first.
  252. assertTrue(jspb.Message.equals(original, copy));
  253. }
  254. /**
  255. * Helper: verify that all expected extensions are present.
  256. * @param {!proto.jspb.test.TestExtendable} msg
  257. */
  258. function checkExtensions(msg) {
  259. assertEquals(0, msg.getExtension(proto.jspb.test.extendOptionalInt32));
  260. assertEquals(-0x7fffffff00000000,
  261. msg.getExtension(proto.jspb.test.extendOptionalInt64));
  262. assertEquals(0x80000000,
  263. msg.getExtension(proto.jspb.test.extendOptionalUint32));
  264. assertEquals(0xf000000000000000,
  265. msg.getExtension(proto.jspb.test.extendOptionalUint64));
  266. assertEquals(-100,
  267. msg.getExtension(proto.jspb.test.extendOptionalSint32));
  268. assertEquals(-0x8000000000000000,
  269. msg.getExtension(proto.jspb.test.extendOptionalSint64));
  270. assertEquals(1234,
  271. msg.getExtension(proto.jspb.test.extendOptionalFixed32));
  272. assertEquals(0x1234567800000000,
  273. msg.getExtension(proto.jspb.test.extendOptionalFixed64));
  274. assertEquals(-1234,
  275. msg.getExtension(proto.jspb.test.extendOptionalSfixed32));
  276. assertEquals(-0x1234567800000000,
  277. msg.getExtension(proto.jspb.test.extendOptionalSfixed64));
  278. assertEquals(1.5,
  279. msg.getExtension(proto.jspb.test.extendOptionalFloat));
  280. assertEquals(-1.5,
  281. msg.getExtension(proto.jspb.test.extendOptionalDouble));
  282. assertEquals(true,
  283. msg.getExtension(proto.jspb.test.extendOptionalBool));
  284. assertEquals('hello world',
  285. msg.getExtension(proto.jspb.test.extendOptionalString));
  286. assertEquals(
  287. true, bytesCompare(
  288. msg.getExtension(proto.jspb.test.extendOptionalBytes), BYTES));
  289. assertEquals(16,
  290. msg.getExtension(
  291. proto.jspb.test.ExtendsWithMessage.optionalExtension).getFoo());
  292. assertElementsEquals(
  293. msg.getExtension(proto.jspb.test.extendRepeatedInt32List),
  294. [-42]);
  295. assertElementsEquals(
  296. msg.getExtension(proto.jspb.test.extendRepeatedInt64List),
  297. [-0x7fffffff00000000]);
  298. assertElementsEquals(
  299. msg.getExtension(proto.jspb.test.extendRepeatedUint32List),
  300. [0x80000000]);
  301. assertElementsEquals(
  302. msg.getExtension(proto.jspb.test.extendRepeatedUint64List),
  303. [0xf000000000000000]);
  304. assertElementsEquals(
  305. msg.getExtension(proto.jspb.test.extendRepeatedSint32List),
  306. [-100]);
  307. assertElementsEquals(
  308. msg.getExtension(proto.jspb.test.extendRepeatedSint64List),
  309. [-0x8000000000000000]);
  310. assertElementsEquals(
  311. msg.getExtension(proto.jspb.test.extendRepeatedFixed32List),
  312. [1234]);
  313. assertElementsEquals(
  314. msg.getExtension(proto.jspb.test.extendRepeatedFixed64List),
  315. [0x1234567800000000]);
  316. assertElementsEquals(
  317. msg.getExtension(proto.jspb.test.extendRepeatedSfixed32List),
  318. [-1234]);
  319. assertElementsEquals(
  320. msg.getExtension(proto.jspb.test.extendRepeatedSfixed64List),
  321. [-0x1234567800000000]);
  322. assertElementsEquals(
  323. msg.getExtension(proto.jspb.test.extendRepeatedFloatList),
  324. [1.5]);
  325. assertElementsEquals(
  326. msg.getExtension(proto.jspb.test.extendRepeatedDoubleList),
  327. [-1.5]);
  328. assertElementsEquals(
  329. msg.getExtension(proto.jspb.test.extendRepeatedBoolList),
  330. [true]);
  331. assertElementsEquals(
  332. msg.getExtension(proto.jspb.test.extendRepeatedStringList),
  333. ['hello world']);
  334. assertEquals(
  335. true,
  336. bytesCompare(
  337. msg.getExtension(proto.jspb.test.extendRepeatedBytesList)[0], BYTES));
  338. assertEquals(1000,
  339. msg.getExtension(
  340. proto.jspb.test.ExtendsWithMessage.repeatedExtensionList)[0]
  341. .getFoo());
  342. assertElementsEquals(
  343. msg.getExtension(proto.jspb.test.extendRepeatedForeignEnumList),
  344. [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
  345. assertElementsEquals(
  346. msg.getExtension(proto.jspb.test.extendPackedRepeatedInt32List),
  347. [-42]);
  348. assertElementsEquals(
  349. msg.getExtension(proto.jspb.test.extendPackedRepeatedInt64List),
  350. [-0x7fffffff00000000]);
  351. assertElementsEquals(
  352. msg.getExtension(proto.jspb.test.extendPackedRepeatedUint32List),
  353. [0x80000000]);
  354. assertElementsEquals(
  355. msg.getExtension(proto.jspb.test.extendPackedRepeatedUint64List),
  356. [0xf000000000000000]);
  357. assertElementsEquals(
  358. msg.getExtension(proto.jspb.test.extendPackedRepeatedSint32List),
  359. [-100]);
  360. assertElementsEquals(
  361. msg.getExtension(proto.jspb.test.extendPackedRepeatedSint64List),
  362. [-0x8000000000000000]);
  363. assertElementsEquals(
  364. msg.getExtension(proto.jspb.test.extendPackedRepeatedFixed32List),
  365. [1234]);
  366. assertElementsEquals(
  367. msg.getExtension(proto.jspb.test.extendPackedRepeatedFixed64List),
  368. [0x1234567800000000]);
  369. assertElementsEquals(
  370. msg.getExtension(proto.jspb.test.extendPackedRepeatedSfixed32List),
  371. [-1234]);
  372. assertElementsEquals(
  373. msg.getExtension(proto.jspb.test.extendPackedRepeatedSfixed64List),
  374. [-0x1234567800000000]);
  375. assertElementsEquals(
  376. msg.getExtension(proto.jspb.test.extendPackedRepeatedFloatList),
  377. [1.5]);
  378. assertElementsEquals(
  379. msg.getExtension(proto.jspb.test.extendPackedRepeatedDoubleList),
  380. [-1.5]);
  381. assertElementsEquals(
  382. msg.getExtension(proto.jspb.test.extendPackedRepeatedBoolList),
  383. [true]);
  384. assertElementsEquals(
  385. msg.getExtension(proto.jspb.test.extendPackedRepeatedForeignEnumList),
  386. [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
  387. }
  388. describe('protoBinaryTest', function() {
  389. /**
  390. * Tests a basic serialization-deserializaton round-trip with all supported
  391. * field types (on the TestAllTypes message type).
  392. */
  393. it('testRoundTrip', function() {
  394. var msg = new proto.jspb.test.TestAllTypes();
  395. fillAllFields(msg);
  396. var encoded = msg.serializeBinary();
  397. var decoded = proto.jspb.test.TestAllTypes.deserializeBinary(encoded);
  398. checkAllFields(msg, decoded);
  399. });
  400. /**
  401. * Test that base64 string and Uint8Array are interchangeable in bytes fields.
  402. */
  403. it('testBytesFieldsGettersInterop', function() {
  404. var msg = new proto.jspb.test.TestAllTypes();
  405. // Set from a base64 string and check all the getters work.
  406. msg.setOptionalBytes(BYTES_B64);
  407. assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
  408. assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
  409. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  410. // Test binary serialize round trip doesn't break it.
  411. msg = proto.jspb.test.TestAllTypes.deserializeBinary(msg.serializeBinary());
  412. assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
  413. assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
  414. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  415. msg = new proto.jspb.test.TestAllTypes();
  416. // Set from a Uint8Array and check all the getters work.
  417. msg.setOptionalBytes(BYTES);
  418. assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
  419. assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
  420. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  421. });
  422. /**
  423. * Test that bytes setters will receive result of any of the getters.
  424. */
  425. it('testBytesFieldsSettersInterop', function() {
  426. var msg = new proto.jspb.test.TestAllTypes();
  427. msg.setOptionalBytes(BYTES);
  428. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  429. msg.setOptionalBytes(msg.getOptionalBytes());
  430. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  431. msg.setOptionalBytes(msg.getOptionalBytes_asB64());
  432. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  433. msg.setOptionalBytes(msg.getOptionalBytes_asU8());
  434. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  435. });
  436. /**
  437. * Test that bytes setters will receive result of any of the getters.
  438. */
  439. it('testRepeatedBytesGetters', function() {
  440. var msg = new proto.jspb.test.TestAllTypes();
  441. function assertGetters() {
  442. assertTrue(typeof msg.getRepeatedBytesList_asB64()[0] === 'string');
  443. assertTrue(typeof msg.getRepeatedBytesList_asB64()[1] === 'string');
  444. assertTrue(msg.getRepeatedBytesList_asU8()[0] instanceof Uint8Array);
  445. assertTrue(msg.getRepeatedBytesList_asU8()[1] instanceof Uint8Array);
  446. assertTrue(bytesCompare(msg.getRepeatedBytesList()[0], BYTES));
  447. assertTrue(bytesCompare(msg.getRepeatedBytesList()[1], BYTES));
  448. assertTrue(bytesCompare(msg.getRepeatedBytesList_asB64()[0], BYTES));
  449. assertTrue(bytesCompare(msg.getRepeatedBytesList_asB64()[1], BYTES));
  450. assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[0], BYTES));
  451. assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[1], BYTES));
  452. }
  453. msg.setRepeatedBytesList([BYTES, BYTES]);
  454. assertGetters();
  455. msg.setRepeatedBytesList([BYTES_B64, BYTES_B64]);
  456. assertGetters();
  457. msg.setRepeatedBytesList([]);
  458. assertEquals(0, msg.getRepeatedBytesList().length);
  459. assertEquals(0, msg.getRepeatedBytesList_asB64().length);
  460. assertEquals(0, msg.getRepeatedBytesList_asU8().length);
  461. });
  462. /**
  463. * Helper: fill all extension values.
  464. * @param {proto.jspb.test.TestExtendable} msg
  465. */
  466. function fillExtensions(msg) {
  467. msg.setExtension(proto.jspb.test.extendOptionalInt32, 0);
  468. msg.setExtension(
  469. proto.jspb.test.extendOptionalInt64, -0x7fffffff00000000);
  470. msg.setExtension(
  471. proto.jspb.test.extendOptionalUint32, 0x80000000);
  472. msg.setExtension(
  473. proto.jspb.test.extendOptionalUint64, 0xf000000000000000);
  474. msg.setExtension(
  475. proto.jspb.test.extendOptionalSint32, -100);
  476. msg.setExtension(
  477. proto.jspb.test.extendOptionalSint64, -0x8000000000000000);
  478. msg.setExtension(
  479. proto.jspb.test.extendOptionalFixed32, 1234);
  480. msg.setExtension(
  481. proto.jspb.test.extendOptionalFixed64, 0x1234567800000000);
  482. msg.setExtension(
  483. proto.jspb.test.extendOptionalSfixed32, -1234);
  484. msg.setExtension(
  485. proto.jspb.test.extendOptionalSfixed64, -0x1234567800000000);
  486. msg.setExtension(
  487. proto.jspb.test.extendOptionalFloat, 1.5);
  488. msg.setExtension(
  489. proto.jspb.test.extendOptionalDouble, -1.5);
  490. msg.setExtension(
  491. proto.jspb.test.extendOptionalBool, true);
  492. msg.setExtension(
  493. proto.jspb.test.extendOptionalString, 'hello world');
  494. msg.setExtension(proto.jspb.test.extendOptionalBytes, BYTES);
  495. var submsg = new proto.jspb.test.ExtendsWithMessage();
  496. submsg.setFoo(16);
  497. msg.setExtension(
  498. proto.jspb.test.ExtendsWithMessage.optionalExtension, submsg);
  499. msg.setExtension(
  500. proto.jspb.test.extendOptionalForeignEnum,
  501. proto.jspb.test.ForeignEnum.FOREIGN_FOO);
  502. msg.setExtension(
  503. proto.jspb.test.extendRepeatedInt32List, [-42]);
  504. msg.setExtension(
  505. proto.jspb.test.extendRepeatedInt64List, [-0x7fffffff00000000]);
  506. msg.setExtension(
  507. proto.jspb.test.extendRepeatedUint32List, [0x80000000]);
  508. msg.setExtension(
  509. proto.jspb.test.extendRepeatedUint64List, [0xf000000000000000]);
  510. msg.setExtension(
  511. proto.jspb.test.extendRepeatedSint32List, [-100]);
  512. msg.setExtension(
  513. proto.jspb.test.extendRepeatedSint64List, [-0x8000000000000000]);
  514. msg.setExtension(
  515. proto.jspb.test.extendRepeatedFixed32List, [1234]);
  516. msg.setExtension(
  517. proto.jspb.test.extendRepeatedFixed64List, [0x1234567800000000]);
  518. msg.setExtension(
  519. proto.jspb.test.extendRepeatedSfixed32List, [-1234]);
  520. msg.setExtension(
  521. proto.jspb.test.extendRepeatedSfixed64List, [-0x1234567800000000]);
  522. msg.setExtension(
  523. proto.jspb.test.extendRepeatedFloatList, [1.5]);
  524. msg.setExtension(
  525. proto.jspb.test.extendRepeatedDoubleList, [-1.5]);
  526. msg.setExtension(
  527. proto.jspb.test.extendRepeatedBoolList, [true]);
  528. msg.setExtension(
  529. proto.jspb.test.extendRepeatedStringList, ['hello world']);
  530. msg.setExtension(proto.jspb.test.extendRepeatedBytesList, [BYTES]);
  531. submsg = new proto.jspb.test.ExtendsWithMessage();
  532. submsg.setFoo(1000);
  533. msg.setExtension(
  534. proto.jspb.test.ExtendsWithMessage.repeatedExtensionList, [submsg]);
  535. msg.setExtension(proto.jspb.test.extendRepeatedForeignEnumList,
  536. [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
  537. msg.setExtension(
  538. proto.jspb.test.extendPackedRepeatedInt32List, [-42]);
  539. msg.setExtension(
  540. proto.jspb.test.extendPackedRepeatedInt64List, [-0x7fffffff00000000]);
  541. msg.setExtension(
  542. proto.jspb.test.extendPackedRepeatedUint32List, [0x80000000]);
  543. msg.setExtension(
  544. proto.jspb.test.extendPackedRepeatedUint64List, [0xf000000000000000]);
  545. msg.setExtension(
  546. proto.jspb.test.extendPackedRepeatedSint32List, [-100]);
  547. msg.setExtension(
  548. proto.jspb.test.extendPackedRepeatedSint64List, [-0x8000000000000000]);
  549. msg.setExtension(
  550. proto.jspb.test.extendPackedRepeatedFixed32List, [1234]);
  551. msg.setExtension(
  552. proto.jspb.test.extendPackedRepeatedFixed64List, [0x1234567800000000]);
  553. msg.setExtension(
  554. proto.jspb.test.extendPackedRepeatedSfixed32List, [-1234]);
  555. msg.setExtension(
  556. proto.jspb.test.extendPackedRepeatedSfixed64List,
  557. [-0x1234567800000000]);
  558. msg.setExtension(
  559. proto.jspb.test.extendPackedRepeatedFloatList, [1.5]);
  560. msg.setExtension(
  561. proto.jspb.test.extendPackedRepeatedDoubleList, [-1.5]);
  562. msg.setExtension(
  563. proto.jspb.test.extendPackedRepeatedBoolList, [true]);
  564. msg.setExtension(proto.jspb.test.extendPackedRepeatedForeignEnumList,
  565. [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
  566. }
  567. /**
  568. * Tests extension serialization and deserialization.
  569. */
  570. it('testExtensions', function() {
  571. var msg = new proto.jspb.test.TestExtendable();
  572. fillExtensions(msg);
  573. var encoded = msg.serializeBinary();
  574. var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded);
  575. checkExtensions(decoded);
  576. });
  577. /**
  578. * Tests that unknown extensions don't cause deserialization failure.
  579. */
  580. it('testUnknownExtension', function() {
  581. var msg = new proto.jspb.test.TestExtendable();
  582. fillExtensions(msg);
  583. var writer = new jspb.BinaryWriter();
  584. writer.writeBool((1 << 29) - 1, true);
  585. proto.jspb.test.TestExtendable.serializeBinaryToWriter(msg, writer);
  586. var encoded = writer.getResultBuffer();
  587. var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded);
  588. checkExtensions(decoded);
  589. });
  590. it('testAnyWellKnownType', function() {
  591. var any = new proto.google.protobuf.Any();
  592. var msg = new proto.jspb.test.TestAllTypes();
  593. fillAllFields(msg);
  594. any.pack(msg.serializeBinary(), 'jspb.test.TestAllTypes');
  595. assertEquals('type.googleapis.com/jspb.test.TestAllTypes',
  596. any.getTypeUrl());
  597. var msg2 = any.unpack(
  598. proto.jspb.test.TestAllTypes.deserializeBinary,
  599. 'jspb.test.TestAllTypes');
  600. checkAllFields(msg, msg2);
  601. });
  602. });