proto3_test.js 15 KB

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