proto3_test.js 14 KB

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