proto3_test.js 16 KB

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