proto3_test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. goog.require('goog.crypt.base64');
  31. goog.require('goog.testing.asserts');
  32. // CommonJS-LoadFromFile: testbinary_pb proto.jspb.test
  33. goog.require('proto.jspb.test.ForeignMessage');
  34. // CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test
  35. goog.require('proto.jspb.test.Proto3Enum');
  36. goog.require('proto.jspb.test.TestProto3');
  37. // CommonJS-LoadFromFile: google/protobuf/timestamp_pb proto.google.protobuf
  38. goog.require('proto.google.protobuf.Timestamp');
  39. // CommonJS-LoadFromFile: google/protobuf/struct_pb proto.google.protobuf
  40. goog.require('proto.google.protobuf.Struct');
  41. var BYTES = new Uint8Array([1, 2, 8, 9]);
  42. var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES);
  43. /**
  44. * Helper: compare a bytes field to an expected value
  45. * @param {Uint8Array|string} arr
  46. * @param {Uint8Array} expected
  47. * @return {boolean}
  48. */
  49. function bytesCompare(arr, expected) {
  50. if (goog.isString(arr)) {
  51. arr = goog.crypt.base64.decodeStringToUint8Array(arr);
  52. }
  53. if (arr.length != expected.length) {
  54. return false;
  55. }
  56. for (var i = 0; i < arr.length; i++) {
  57. if (arr[i] != expected[i]) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. describe('proto3Test', function() {
  64. /**
  65. * Test default values don't affect equality test.
  66. */
  67. it('testEqualsProto3', function() {
  68. var msg1 = new proto.jspb.test.TestProto3();
  69. var msg2 = new proto.jspb.test.TestProto3();
  70. msg2.setOptionalString('');
  71. assertTrue(jspb.Message.equals(msg1, msg2));
  72. });
  73. /**
  74. * Test setting when a field has default semantics.
  75. */
  76. it('testSetProto3ToValueAndBackToDefault', function() {
  77. var msg = new proto.jspb.test.TestProto3();
  78. // Setting should work normally.
  79. msg.setOptionalString('optionalString');
  80. assertEquals(msg.getOptionalString(), 'optionalString');
  81. // Clearing should work too ...
  82. msg.setOptionalString('');
  83. assertEquals(msg.getOptionalString(), '');
  84. // ... and shouldn't affect the equality with a brand new message.
  85. assertTrue(jspb.Message.equals(msg, new proto.jspb.test.TestProto3()));
  86. });
  87. /**
  88. * Test defaults for proto3 message fields.
  89. */
  90. it('testProto3FieldDefaults', function() {
  91. var msg = new proto.jspb.test.TestProto3();
  92. assertEquals(msg.getOptionalInt32(), 0);
  93. assertEquals(msg.getOptionalInt64(), 0);
  94. assertEquals(msg.getOptionalUint32(), 0);
  95. assertEquals(msg.getOptionalUint64(), 0);
  96. assertEquals(msg.getOptionalSint32(), 0);
  97. assertEquals(msg.getOptionalSint64(), 0);
  98. assertEquals(msg.getOptionalFixed32(), 0);
  99. assertEquals(msg.getOptionalFixed64(), 0);
  100. assertEquals(msg.getOptionalSfixed32(), 0);
  101. assertEquals(msg.getOptionalSfixed64(), 0);
  102. assertEquals(msg.getOptionalFloat(), 0);
  103. assertEquals(msg.getOptionalDouble(), 0);
  104. assertEquals(msg.getOptionalString(), '');
  105. // TODO(b/26173701): when we change bytes fields default getter to return
  106. // Uint8Array, we'll want to switch this assertion to match the u8 case.
  107. assertEquals(typeof msg.getOptionalBytes(), 'string');
  108. assertEquals(msg.getOptionalBytes_asU8() instanceof Uint8Array, true);
  109. assertEquals(typeof msg.getOptionalBytes_asB64(), 'string');
  110. assertEquals(msg.getOptionalBytes().length, 0);
  111. assertEquals(msg.getOptionalBytes_asU8().length, 0);
  112. assertEquals(msg.getOptionalBytes_asB64(), '');
  113. assertEquals(msg.getOptionalForeignEnum(),
  114. proto.jspb.test.Proto3Enum.PROTO3_FOO);
  115. assertEquals(msg.getOptionalForeignMessage(), undefined);
  116. assertEquals(msg.getOptionalForeignMessage(), undefined);
  117. assertEquals(msg.getRepeatedInt32List().length, 0);
  118. assertEquals(msg.getRepeatedInt64List().length, 0);
  119. assertEquals(msg.getRepeatedUint32List().length, 0);
  120. assertEquals(msg.getRepeatedUint64List().length, 0);
  121. assertEquals(msg.getRepeatedSint32List().length, 0);
  122. assertEquals(msg.getRepeatedSint64List().length, 0);
  123. assertEquals(msg.getRepeatedFixed32List().length, 0);
  124. assertEquals(msg.getRepeatedFixed64List().length, 0);
  125. assertEquals(msg.getRepeatedSfixed32List().length, 0);
  126. assertEquals(msg.getRepeatedSfixed64List().length, 0);
  127. assertEquals(msg.getRepeatedFloatList().length, 0);
  128. assertEquals(msg.getRepeatedDoubleList().length, 0);
  129. assertEquals(msg.getRepeatedStringList().length, 0);
  130. assertEquals(msg.getRepeatedBytesList().length, 0);
  131. assertEquals(msg.getRepeatedForeignEnumList().length, 0);
  132. assertEquals(msg.getRepeatedForeignMessageList().length, 0);
  133. });
  134. /**
  135. * Test that all fields can be set and read via a serialization roundtrip.
  136. */
  137. it('testProto3FieldSetGet', function() {
  138. var msg = new proto.jspb.test.TestProto3();
  139. msg.setOptionalInt32(-42);
  140. msg.setOptionalInt64(-0x7fffffff00000000);
  141. msg.setOptionalUint32(0x80000000);
  142. msg.setOptionalUint64(0xf000000000000000);
  143. msg.setOptionalSint32(-100);
  144. msg.setOptionalSint64(-0x8000000000000000);
  145. msg.setOptionalFixed32(1234);
  146. msg.setOptionalFixed64(0x1234567800000000);
  147. msg.setOptionalSfixed32(-1234);
  148. msg.setOptionalSfixed64(-0x1234567800000000);
  149. msg.setOptionalFloat(1.5);
  150. msg.setOptionalDouble(-1.5);
  151. msg.setOptionalBool(true);
  152. msg.setOptionalString('hello world');
  153. msg.setOptionalBytes(BYTES);
  154. var submsg = new proto.jspb.test.ForeignMessage();
  155. submsg.setC(16);
  156. msg.setOptionalForeignMessage(submsg);
  157. msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
  158. msg.setRepeatedInt32List([-42]);
  159. msg.setRepeatedInt64List([-0x7fffffff00000000]);
  160. msg.setRepeatedUint32List([0x80000000]);
  161. msg.setRepeatedUint64List([0xf000000000000000]);
  162. msg.setRepeatedSint32List([-100]);
  163. msg.setRepeatedSint64List([-0x8000000000000000]);
  164. msg.setRepeatedFixed32List([1234]);
  165. msg.setRepeatedFixed64List([0x1234567800000000]);
  166. msg.setRepeatedSfixed32List([-1234]);
  167. msg.setRepeatedSfixed64List([-0x1234567800000000]);
  168. msg.setRepeatedFloatList([1.5]);
  169. msg.setRepeatedDoubleList([-1.5]);
  170. msg.setRepeatedBoolList([true]);
  171. msg.setRepeatedStringList(['hello world']);
  172. msg.setRepeatedBytesList([BYTES]);
  173. submsg = new proto.jspb.test.ForeignMessage();
  174. submsg.setC(1000);
  175. msg.setRepeatedForeignMessageList([submsg]);
  176. msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]);
  177. msg.setOneofString('asdf');
  178. var serialized = msg.serializeBinary();
  179. msg = proto.jspb.test.TestProto3.deserializeBinary(serialized);
  180. assertEquals(msg.getOptionalInt32(), -42);
  181. assertEquals(msg.getOptionalInt64(), -0x7fffffff00000000);
  182. assertEquals(msg.getOptionalUint32(), 0x80000000);
  183. assertEquals(msg.getOptionalUint64(), 0xf000000000000000);
  184. assertEquals(msg.getOptionalSint32(), -100);
  185. assertEquals(msg.getOptionalSint64(), -0x8000000000000000);
  186. assertEquals(msg.getOptionalFixed32(), 1234);
  187. assertEquals(msg.getOptionalFixed64(), 0x1234567800000000);
  188. assertEquals(msg.getOptionalSfixed32(), -1234);
  189. assertEquals(msg.getOptionalSfixed64(), -0x1234567800000000);
  190. assertEquals(msg.getOptionalFloat(), 1.5);
  191. assertEquals(msg.getOptionalDouble(), -1.5);
  192. assertEquals(msg.getOptionalBool(), true);
  193. assertEquals(msg.getOptionalString(), 'hello world');
  194. assertEquals(true, bytesCompare(msg.getOptionalBytes(), BYTES));
  195. assertEquals(msg.getOptionalForeignMessage().getC(), 16);
  196. assertEquals(msg.getOptionalForeignEnum(),
  197. proto.jspb.test.Proto3Enum.PROTO3_BAR);
  198. assertElementsEquals(msg.getRepeatedInt32List(), [-42]);
  199. assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]);
  200. assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]);
  201. assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]);
  202. assertElementsEquals(msg.getRepeatedSint32List(), [-100]);
  203. assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]);
  204. assertElementsEquals(msg.getRepeatedFixed32List(), [1234]);
  205. assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]);
  206. assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]);
  207. assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]);
  208. assertElementsEquals(msg.getRepeatedFloatList(), [1.5]);
  209. assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]);
  210. assertElementsEquals(msg.getRepeatedBoolList(), [true]);
  211. assertElementsEquals(msg.getRepeatedStringList(), ['hello world']);
  212. assertEquals(msg.getRepeatedBytesList().length, 1);
  213. assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], BYTES));
  214. assertEquals(msg.getRepeatedForeignMessageList().length, 1);
  215. assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000);
  216. assertElementsEquals(msg.getRepeatedForeignEnumList(),
  217. [proto.jspb.test.Proto3Enum.PROTO3_BAR]);
  218. assertEquals(msg.getOneofString(), 'asdf');
  219. });
  220. /**
  221. * Test that oneofs continue to have a notion of field presence.
  222. */
  223. it('testOneofs', function() {
  224. // Default instance.
  225. var msg = new proto.jspb.test.TestProto3();
  226. assertEquals(msg.getOneofUint32(), 0);
  227. assertEquals(msg.getOneofForeignMessage(), undefined);
  228. assertEquals(msg.getOneofString(), '');
  229. assertEquals(msg.getOneofBytes(), '');
  230. assertFalse(msg.hasOneofUint32());
  231. assertFalse(msg.hasOneofForeignMessage());
  232. assertFalse(msg.hasOneofString());
  233. assertFalse(msg.hasOneofBytes());
  234. // Integer field.
  235. msg.setOneofUint32(42);
  236. assertEquals(msg.getOneofUint32(), 42);
  237. assertEquals(msg.getOneofForeignMessage(), undefined);
  238. assertEquals(msg.getOneofString(), '');
  239. assertEquals(msg.getOneofBytes(), '');
  240. assertTrue(msg.hasOneofUint32());
  241. assertFalse(msg.hasOneofForeignMessage());
  242. assertFalse(msg.hasOneofString());
  243. assertFalse(msg.hasOneofBytes());
  244. // Sub-message field.
  245. var submsg = new proto.jspb.test.ForeignMessage();
  246. msg.setOneofForeignMessage(submsg);
  247. assertEquals(msg.getOneofUint32(), 0);
  248. assertEquals(msg.getOneofForeignMessage(), submsg);
  249. assertEquals(msg.getOneofString(), '');
  250. assertEquals(msg.getOneofBytes(), '');
  251. assertFalse(msg.hasOneofUint32());
  252. assertTrue(msg.hasOneofForeignMessage());
  253. assertFalse(msg.hasOneofString());
  254. assertFalse(msg.hasOneofBytes());
  255. // String field.
  256. msg.setOneofString('hello');
  257. assertEquals(msg.getOneofUint32(), 0);
  258. assertEquals(msg.getOneofForeignMessage(), undefined);
  259. assertEquals(msg.getOneofString(), 'hello');
  260. assertEquals(msg.getOneofBytes(), '');
  261. assertFalse(msg.hasOneofUint32());
  262. assertFalse(msg.hasOneofForeignMessage());
  263. assertTrue(msg.hasOneofString());
  264. assertFalse(msg.hasOneofBytes());
  265. // Bytes field.
  266. msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
  267. assertEquals(msg.getOneofUint32(), 0);
  268. assertEquals(msg.getOneofForeignMessage(), undefined);
  269. assertEquals(msg.getOneofString(), '');
  270. assertEquals(msg.getOneofBytes_asB64(),
  271. goog.crypt.base64.encodeString('\u00FF\u00FF'));
  272. assertFalse(msg.hasOneofUint32());
  273. assertFalse(msg.hasOneofForeignMessage());
  274. assertFalse(msg.hasOneofString());
  275. assertTrue(msg.hasOneofBytes());
  276. });
  277. /**
  278. * Test that "default"-valued primitive fields are not emitted on the wire.
  279. */
  280. it('testNoSerializeDefaults', function() {
  281. var msg = new proto.jspb.test.TestProto3();
  282. // Set each primitive to a non-default value, then back to its default, to
  283. // ensure that the serialization is actually checking the value and not just
  284. // whether it has ever been set.
  285. msg.setOptionalInt32(42);
  286. msg.setOptionalInt32(0);
  287. msg.setOptionalDouble(3.14);
  288. msg.setOptionalDouble(0.0);
  289. msg.setOptionalBool(true);
  290. msg.setOptionalBool(false);
  291. msg.setOptionalString('hello world');
  292. msg.setOptionalString('');
  293. msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
  294. msg.setOptionalBytes('');
  295. msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage());
  296. msg.setOptionalForeignMessage(null);
  297. msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
  298. msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO);
  299. msg.setOneofUint32(32);
  300. msg.clearOneofUint32();
  301. var serialized = msg.serializeBinary();
  302. assertEquals(0, serialized.length);
  303. });
  304. /**
  305. * Test that base64 string and Uint8Array are interchangeable in bytes fields.
  306. */
  307. it('testBytesFieldsInterop', function() {
  308. var msg = new proto.jspb.test.TestProto3();
  309. // Set as a base64 string and check all the getters work.
  310. msg.setOptionalBytes(BYTES_B64);
  311. assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
  312. assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
  313. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  314. // Test binary serialize round trip doesn't break it.
  315. msg = proto.jspb.test.TestProto3.deserializeBinary(msg.serializeBinary());
  316. assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
  317. assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
  318. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  319. msg = new proto.jspb.test.TestProto3();
  320. // Set as a Uint8Array and check all the getters work.
  321. msg.setOptionalBytes(BYTES);
  322. assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
  323. assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
  324. assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  325. });
  326. it('testTimestampWellKnownType', function() {
  327. var msg = new proto.google.protobuf.Timestamp();
  328. msg.fromDate(new Date(123456789));
  329. assertEquals(123456, msg.getSeconds());
  330. assertEquals(789000000, msg.getNanos());
  331. var date = msg.toDate();
  332. assertEquals(123456789, date.getTime());
  333. });
  334. it('testStructWellKnownType', function() {
  335. var jsObj = {
  336. abc: "def",
  337. number: 12345.678,
  338. nullKey: null,
  339. boolKey: true,
  340. listKey: [1, null, true, false, "abc"],
  341. structKey: {foo: "bar", somenum: 123},
  342. complicatedKey: [{xyz: {abc: [3, 4, null, false]}}, "zzz"]
  343. };
  344. var struct = proto.google.protobuf.Struct.fromJavaScript(jsObj);
  345. var jsObj2 = struct.toJavaScript();
  346. assertEquals("def", jsObj2.abc);
  347. assertEquals(12345.678, jsObj2.number);
  348. assertEquals(null, jsObj2.nullKey);
  349. assertEquals(true, jsObj2.boolKey);
  350. assertEquals("abc", jsObj2.listKey[4]);
  351. assertEquals("bar", jsObj2.structKey.foo);
  352. assertEquals(4, jsObj2.complicatedKey[0].xyz.abc[1]);
  353. });
  354. });