proto3_test.js 13 KB

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