blob: aee327a8ea27830d9f65d45537977c23a6b8f3be [file] [log] [blame]
Feng Xiaoe841bac2015-12-11 17:09:20 -08001// 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
31// Test suite is written using Jasmine -- see http://jasmine.github.io/
32
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070033goog.require('goog.crypt.base64');
Feng Xiaoe841bac2015-12-11 17:09:20 -080034goog.require('goog.testing.asserts');
Adam Cozzette5a76e632016-11-17 16:48:38 -080035goog.require('jspb.BinaryWriter');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070036goog.require('jspb.Message');
Josh Habermane9f31ee2016-02-04 10:29:27 -080037
murgatroid99a862b6b2016-02-24 13:44:57 -080038// CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test
Feng Xiaoe841bac2015-12-11 17:09:20 -080039goog.require('proto.jspb.test.ExtendsWithMessage');
40goog.require('proto.jspb.test.ForeignEnum');
41goog.require('proto.jspb.test.ForeignMessage');
42goog.require('proto.jspb.test.TestAllTypes');
43goog.require('proto.jspb.test.TestExtendable');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070044goog.require('proto.jspb.test.extendOptionalBool');
45goog.require('proto.jspb.test.extendOptionalBytes');
46goog.require('proto.jspb.test.extendOptionalDouble');
47goog.require('proto.jspb.test.extendOptionalFixed32');
48goog.require('proto.jspb.test.extendOptionalFixed64');
49goog.require('proto.jspb.test.extendOptionalFloat');
50goog.require('proto.jspb.test.extendOptionalForeignEnum');
51goog.require('proto.jspb.test.extendOptionalInt32');
52goog.require('proto.jspb.test.extendOptionalInt64');
53goog.require('proto.jspb.test.extendOptionalSfixed32');
54goog.require('proto.jspb.test.extendOptionalSfixed64');
55goog.require('proto.jspb.test.extendOptionalSint32');
56goog.require('proto.jspb.test.extendOptionalSint64');
57goog.require('proto.jspb.test.extendOptionalString');
58goog.require('proto.jspb.test.extendOptionalUint32');
59goog.require('proto.jspb.test.extendOptionalUint64');
60goog.require('proto.jspb.test.extendPackedRepeatedBoolList');
61goog.require('proto.jspb.test.extendPackedRepeatedDoubleList');
62goog.require('proto.jspb.test.extendPackedRepeatedFixed32List');
63goog.require('proto.jspb.test.extendPackedRepeatedFixed64List');
64goog.require('proto.jspb.test.extendPackedRepeatedFloatList');
65goog.require('proto.jspb.test.extendPackedRepeatedForeignEnumList');
66goog.require('proto.jspb.test.extendPackedRepeatedInt32List');
67goog.require('proto.jspb.test.extendPackedRepeatedInt64List');
68goog.require('proto.jspb.test.extendPackedRepeatedSfixed32List');
69goog.require('proto.jspb.test.extendPackedRepeatedSfixed64List');
70goog.require('proto.jspb.test.extendPackedRepeatedSint32List');
71goog.require('proto.jspb.test.extendPackedRepeatedSint64List');
72goog.require('proto.jspb.test.extendPackedRepeatedUint32List');
73goog.require('proto.jspb.test.extendPackedRepeatedUint64List');
74goog.require('proto.jspb.test.extendRepeatedBoolList');
75goog.require('proto.jspb.test.extendRepeatedBytesList');
76goog.require('proto.jspb.test.extendRepeatedDoubleList');
77goog.require('proto.jspb.test.extendRepeatedFixed32List');
78goog.require('proto.jspb.test.extendRepeatedFixed64List');
79goog.require('proto.jspb.test.extendRepeatedFloatList');
80goog.require('proto.jspb.test.extendRepeatedForeignEnumList');
81goog.require('proto.jspb.test.extendRepeatedInt32List');
82goog.require('proto.jspb.test.extendRepeatedInt64List');
83goog.require('proto.jspb.test.extendRepeatedSfixed32List');
84goog.require('proto.jspb.test.extendRepeatedSfixed64List');
85goog.require('proto.jspb.test.extendRepeatedSint32List');
86goog.require('proto.jspb.test.extendRepeatedSint64List');
87goog.require('proto.jspb.test.extendRepeatedStringList');
88goog.require('proto.jspb.test.extendRepeatedUint32List');
89goog.require('proto.jspb.test.extendRepeatedUint64List');
90
Adam Cozzette40f35862016-11-18 12:32:52 -080091// CommonJS-LoadFromFile: ../node_modules/google-protobuf/google/protobuf/any_pb proto.google.protobuf
Adam Cozzette5a76e632016-11-17 16:48:38 -080092goog.require('proto.google.protobuf.Any');
93
Feng Xiaoe841bac2015-12-11 17:09:20 -080094
95var suite = {};
96
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070097var BYTES = new Uint8Array([1, 2, 8, 9]);
98
99var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES);
100
101
Feng Xiaoe841bac2015-12-11 17:09:20 -0800102/**
103 * Helper: fill all fields on a TestAllTypes message.
104 * @param {proto.jspb.test.TestAllTypes} msg
105 */
106function fillAllFields(msg) {
107 msg.setOptionalInt32(-42);
108 // can be exactly represented by JS number (64-bit double, i.e., 52-bit
109 // mantissa).
110 msg.setOptionalInt64(-0x7fffffff00000000);
111 msg.setOptionalUint32(0x80000000);
112 msg.setOptionalUint64(0xf000000000000000);
113 msg.setOptionalSint32(-100);
114 msg.setOptionalSint64(-0x8000000000000000);
115 msg.setOptionalFixed32(1234);
116 msg.setOptionalFixed64(0x1234567800000000);
117 msg.setOptionalSfixed32(-1234);
118 msg.setOptionalSfixed64(-0x1234567800000000);
119 msg.setOptionalFloat(1.5);
120 msg.setOptionalDouble(-1.5);
121 msg.setOptionalBool(true);
122 msg.setOptionalString('hello world');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700123 msg.setOptionalBytes(BYTES);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800124 msg.setOptionalGroup(new proto.jspb.test.TestAllTypes.OptionalGroup());
125 msg.getOptionalGroup().setA(100);
126 var submsg = new proto.jspb.test.ForeignMessage();
127 submsg.setC(16);
128 msg.setOptionalForeignMessage(submsg);
129 msg.setOptionalForeignEnum(proto.jspb.test.ForeignEnum.FOREIGN_FOO);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800130 msg.setOneofString('oneof');
131
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700132
Feng Xiaoe841bac2015-12-11 17:09:20 -0800133 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']);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700147 msg.setRepeatedBytesList([BYTES, BYTES]);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800148 msg.setRepeatedGroupList([new proto.jspb.test.TestAllTypes.RepeatedGroup()]);
149 msg.getRepeatedGroupList()[0].setA(100);
150 submsg = new proto.jspb.test.ForeignMessage();
151 submsg.setC(1000);
152 msg.setRepeatedForeignMessageList([submsg]);
153 msg.setRepeatedForeignEnumList([proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
154
155 msg.setPackedRepeatedInt32List([-42]);
156 msg.setPackedRepeatedInt64List([-0x7fffffff00000000]);
157 msg.setPackedRepeatedUint32List([0x80000000]);
158 msg.setPackedRepeatedUint64List([0xf000000000000000]);
159 msg.setPackedRepeatedSint32List([-100]);
160 msg.setPackedRepeatedSint64List([-0x8000000000000000]);
161 msg.setPackedRepeatedFixed32List([1234]);
162 msg.setPackedRepeatedFixed64List([0x1234567800000000]);
163 msg.setPackedRepeatedSfixed32List([-1234]);
164 msg.setPackedRepeatedSfixed64List([-0x1234567800000000]);
165 msg.setPackedRepeatedFloatList([1.5]);
166 msg.setPackedRepeatedDoubleList([-1.5]);
167 msg.setPackedRepeatedBoolList([true]);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700168
Feng Xiaoe841bac2015-12-11 17:09:20 -0800169}
170
171
172/**
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700173 * Helper: compare a bytes field to an expected value
Feng Xiaoe841bac2015-12-11 17:09:20 -0800174 * @param {Uint8Array|string} arr
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700175 * @param {Uint8Array} expected
Feng Xiaoe841bac2015-12-11 17:09:20 -0800176 * @return {boolean}
177 */
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700178function bytesCompare(arr, expected) {
Rafi Kamal58d44202019-11-11 17:06:56 -0800179 if (typeof arr === 'string') {
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700180 arr = goog.crypt.base64.decodeStringToUint8Array(arr);
181 }
182 if (arr.length != expected.length) {
Feng Xiaoe841bac2015-12-11 17:09:20 -0800183 return false;
184 }
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700185 for (var i = 0; i < arr.length; i++) {
186 if (arr[i] != expected[i]) {
187 return false;
Feng Xiaoe841bac2015-12-11 17:09:20 -0800188 }
Feng Xiaoe841bac2015-12-11 17:09:20 -0800189 }
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700190 return true;
Feng Xiaoe841bac2015-12-11 17:09:20 -0800191}
192
193
194/**
195 * Helper: verify contents of given TestAllTypes message as set by
196 * fillAllFields().
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700197 * @param {proto.jspb.test.TestAllTypes} original
198 * @param {proto.jspb.test.TestAllTypes} copy
Feng Xiaoe841bac2015-12-11 17:09:20 -0800199 */
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700200function checkAllFields(original, copy) {
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700201 assertEquals(copy.getOptionalInt32(), -42);
202 assertEquals(copy.getOptionalInt64(), -0x7fffffff00000000);
203 assertEquals(copy.getOptionalUint32(), 0x80000000);
204 assertEquals(copy.getOptionalUint64(), 0xf000000000000000);
205 assertEquals(copy.getOptionalSint32(), -100);
206 assertEquals(copy.getOptionalSint64(), -0x8000000000000000);
207 assertEquals(copy.getOptionalFixed32(), 1234);
208 assertEquals(copy.getOptionalFixed64(), 0x1234567800000000);
209 assertEquals(copy.getOptionalSfixed32(), -1234);
210 assertEquals(copy.getOptionalSfixed64(), -0x1234567800000000);
211 assertEquals(copy.getOptionalFloat(), 1.5);
212 assertEquals(copy.getOptionalDouble(), -1.5);
213 assertEquals(copy.getOptionalBool(), true);
214 assertEquals(copy.getOptionalString(), 'hello world');
215 assertEquals(true, bytesCompare(copy.getOptionalBytes(), BYTES));
216 assertEquals(true, bytesCompare(copy.getOptionalBytes_asU8(), BYTES));
217 assertEquals(
218 copy.getOptionalBytes_asB64(), goog.crypt.base64.encodeByteArray(BYTES));
219
220 assertEquals(copy.getOptionalGroup().getA(), 100);
221 assertEquals(copy.getOptionalForeignMessage().getC(), 16);
222 assertEquals(copy.getOptionalForeignEnum(),
Feng Xiaoe841bac2015-12-11 17:09:20 -0800223 proto.jspb.test.ForeignEnum.FOREIGN_FOO);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700224
225
226 assertEquals(copy.getOneofString(), 'oneof');
227 assertEquals(copy.getOneofFieldCase(),
Feng Xiaoe841bac2015-12-11 17:09:20 -0800228 proto.jspb.test.TestAllTypes.OneofFieldCase.ONEOF_STRING);
229
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700230 assertElementsEquals(copy.getRepeatedInt32List(), [-42]);
231 assertElementsEquals(copy.getRepeatedInt64List(), [-0x7fffffff00000000]);
232 assertElementsEquals(copy.getRepeatedUint32List(), [0x80000000]);
233 assertElementsEquals(copy.getRepeatedUint64List(), [0xf000000000000000]);
234 assertElementsEquals(copy.getRepeatedSint32List(), [-100]);
235 assertElementsEquals(copy.getRepeatedSint64List(), [-0x8000000000000000]);
236 assertElementsEquals(copy.getRepeatedFixed32List(), [1234]);
237 assertElementsEquals(copy.getRepeatedFixed64List(), [0x1234567800000000]);
238 assertElementsEquals(copy.getRepeatedSfixed32List(), [-1234]);
239 assertElementsEquals(copy.getRepeatedSfixed64List(), [-0x1234567800000000]);
240 assertElementsEquals(copy.getRepeatedFloatList(), [1.5]);
241 assertElementsEquals(copy.getRepeatedDoubleList(), [-1.5]);
242 assertElementsEquals(copy.getRepeatedBoolList(), [true]);
243 assertElementsEquals(copy.getRepeatedStringList(), ['hello world']);
244 assertEquals(copy.getRepeatedBytesList().length, 2);
245 assertEquals(true, bytesCompare(copy.getRepeatedBytesList_asU8()[0], BYTES));
246 assertEquals(true, bytesCompare(copy.getRepeatedBytesList()[0], BYTES));
247 assertEquals(true, bytesCompare(copy.getRepeatedBytesList_asU8()[1], BYTES));
248 assertEquals(copy.getRepeatedBytesList_asB64()[0], BYTES_B64);
249 assertEquals(copy.getRepeatedBytesList_asB64()[1], BYTES_B64);
250 assertEquals(copy.getRepeatedGroupList().length, 1);
251 assertEquals(copy.getRepeatedGroupList()[0].getA(), 100);
252 assertEquals(copy.getRepeatedForeignMessageList().length, 1);
253 assertEquals(copy.getRepeatedForeignMessageList()[0].getC(), 1000);
254 assertElementsEquals(copy.getRepeatedForeignEnumList(),
Feng Xiaoe841bac2015-12-11 17:09:20 -0800255 [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
256
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700257 assertElementsEquals(copy.getPackedRepeatedInt32List(), [-42]);
258 assertElementsEquals(copy.getPackedRepeatedInt64List(),
Feng Xiaoe841bac2015-12-11 17:09:20 -0800259 [-0x7fffffff00000000]);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700260 assertElementsEquals(copy.getPackedRepeatedUint32List(), [0x80000000]);
261 assertElementsEquals(copy.getPackedRepeatedUint64List(),
262 [0xf000000000000000]);
263 assertElementsEquals(copy.getPackedRepeatedSint32List(), [-100]);
264 assertElementsEquals(copy.getPackedRepeatedSint64List(),
Feng Xiaoe841bac2015-12-11 17:09:20 -0800265 [-0x8000000000000000]);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700266 assertElementsEquals(copy.getPackedRepeatedFixed32List(), [1234]);
267 assertElementsEquals(copy.getPackedRepeatedFixed64List(),
Feng Xiaoe841bac2015-12-11 17:09:20 -0800268 [0x1234567800000000]);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700269 assertElementsEquals(copy.getPackedRepeatedSfixed32List(), [-1234]);
270 assertElementsEquals(copy.getPackedRepeatedSfixed64List(),
Feng Xiaoe841bac2015-12-11 17:09:20 -0800271 [-0x1234567800000000]);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700272 assertElementsEquals(copy.getPackedRepeatedFloatList(), [1.5]);
273 assertElementsEquals(copy.getPackedRepeatedDoubleList(), [-1.5]);
274
Adam Cozzette5a76e632016-11-17 16:48:38 -0800275
276 // Check last so we get more granular errors first.
277 assertTrue(jspb.Message.equals(original, copy));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800278}
279
280
281/**
282 * Helper: verify that all expected extensions are present.
283 * @param {!proto.jspb.test.TestExtendable} msg
284 */
285function checkExtensions(msg) {
Adam Cozzette651ba622017-03-06 15:12:19 -0800286 assertEquals(0, msg.getExtension(proto.jspb.test.extendOptionalInt32));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800287 assertEquals(-0x7fffffff00000000,
288 msg.getExtension(proto.jspb.test.extendOptionalInt64));
289 assertEquals(0x80000000,
290 msg.getExtension(proto.jspb.test.extendOptionalUint32));
291 assertEquals(0xf000000000000000,
292 msg.getExtension(proto.jspb.test.extendOptionalUint64));
293 assertEquals(-100,
294 msg.getExtension(proto.jspb.test.extendOptionalSint32));
295 assertEquals(-0x8000000000000000,
296 msg.getExtension(proto.jspb.test.extendOptionalSint64));
297 assertEquals(1234,
298 msg.getExtension(proto.jspb.test.extendOptionalFixed32));
299 assertEquals(0x1234567800000000,
300 msg.getExtension(proto.jspb.test.extendOptionalFixed64));
301 assertEquals(-1234,
302 msg.getExtension(proto.jspb.test.extendOptionalSfixed32));
303 assertEquals(-0x1234567800000000,
304 msg.getExtension(proto.jspb.test.extendOptionalSfixed64));
305 assertEquals(1.5,
306 msg.getExtension(proto.jspb.test.extendOptionalFloat));
307 assertEquals(-1.5,
308 msg.getExtension(proto.jspb.test.extendOptionalDouble));
309 assertEquals(true,
310 msg.getExtension(proto.jspb.test.extendOptionalBool));
311 assertEquals('hello world',
312 msg.getExtension(proto.jspb.test.extendOptionalString));
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700313 assertEquals(
314 true, bytesCompare(
315 msg.getExtension(proto.jspb.test.extendOptionalBytes), BYTES));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800316 assertEquals(16,
317 msg.getExtension(
318 proto.jspb.test.ExtendsWithMessage.optionalExtension).getFoo());
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700319
Feng Xiaoe841bac2015-12-11 17:09:20 -0800320
321 assertElementsEquals(
322 msg.getExtension(proto.jspb.test.extendRepeatedInt32List),
323 [-42]);
324 assertElementsEquals(
325 msg.getExtension(proto.jspb.test.extendRepeatedInt64List),
326 [-0x7fffffff00000000]);
327 assertElementsEquals(
328 msg.getExtension(proto.jspb.test.extendRepeatedUint32List),
329 [0x80000000]);
330 assertElementsEquals(
331 msg.getExtension(proto.jspb.test.extendRepeatedUint64List),
332 [0xf000000000000000]);
333 assertElementsEquals(
334 msg.getExtension(proto.jspb.test.extendRepeatedSint32List),
335 [-100]);
336 assertElementsEquals(
337 msg.getExtension(proto.jspb.test.extendRepeatedSint64List),
338 [-0x8000000000000000]);
339 assertElementsEquals(
340 msg.getExtension(proto.jspb.test.extendRepeatedFixed32List),
341 [1234]);
342 assertElementsEquals(
343 msg.getExtension(proto.jspb.test.extendRepeatedFixed64List),
344 [0x1234567800000000]);
345 assertElementsEquals(
346 msg.getExtension(proto.jspb.test.extendRepeatedSfixed32List),
347 [-1234]);
348 assertElementsEquals(
349 msg.getExtension(proto.jspb.test.extendRepeatedSfixed64List),
350 [-0x1234567800000000]);
351 assertElementsEquals(
352 msg.getExtension(proto.jspb.test.extendRepeatedFloatList),
353 [1.5]);
354 assertElementsEquals(
355 msg.getExtension(proto.jspb.test.extendRepeatedDoubleList),
356 [-1.5]);
357 assertElementsEquals(
358 msg.getExtension(proto.jspb.test.extendRepeatedBoolList),
359 [true]);
360 assertElementsEquals(
361 msg.getExtension(proto.jspb.test.extendRepeatedStringList),
362 ['hello world']);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700363 assertEquals(
364 true,
Feng Xiaoe841bac2015-12-11 17:09:20 -0800365 bytesCompare(
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700366 msg.getExtension(proto.jspb.test.extendRepeatedBytesList)[0], BYTES));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800367 assertEquals(1000,
368 msg.getExtension(
369 proto.jspb.test.ExtendsWithMessage.repeatedExtensionList)[0]
370 .getFoo());
371 assertElementsEquals(
372 msg.getExtension(proto.jspb.test.extendRepeatedForeignEnumList),
373 [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
374
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700375
Feng Xiaoe841bac2015-12-11 17:09:20 -0800376 assertElementsEquals(
Feng Xiaoe841bac2015-12-11 17:09:20 -0800377 msg.getExtension(proto.jspb.test.extendPackedRepeatedInt32List),
378 [-42]);
379 assertElementsEquals(
380 msg.getExtension(proto.jspb.test.extendPackedRepeatedInt64List),
381 [-0x7fffffff00000000]);
382 assertElementsEquals(
383 msg.getExtension(proto.jspb.test.extendPackedRepeatedUint32List),
384 [0x80000000]);
385 assertElementsEquals(
386 msg.getExtension(proto.jspb.test.extendPackedRepeatedUint64List),
387 [0xf000000000000000]);
388 assertElementsEquals(
389 msg.getExtension(proto.jspb.test.extendPackedRepeatedSint32List),
390 [-100]);
391 assertElementsEquals(
392 msg.getExtension(proto.jspb.test.extendPackedRepeatedSint64List),
393 [-0x8000000000000000]);
394 assertElementsEquals(
395 msg.getExtension(proto.jspb.test.extendPackedRepeatedFixed32List),
396 [1234]);
397 assertElementsEquals(
398 msg.getExtension(proto.jspb.test.extendPackedRepeatedFixed64List),
399 [0x1234567800000000]);
400 assertElementsEquals(
401 msg.getExtension(proto.jspb.test.extendPackedRepeatedSfixed32List),
402 [-1234]);
403 assertElementsEquals(
404 msg.getExtension(proto.jspb.test.extendPackedRepeatedSfixed64List),
405 [-0x1234567800000000]);
406 assertElementsEquals(
407 msg.getExtension(proto.jspb.test.extendPackedRepeatedFloatList),
408 [1.5]);
409 assertElementsEquals(
410 msg.getExtension(proto.jspb.test.extendPackedRepeatedDoubleList),
411 [-1.5]);
412 assertElementsEquals(
413 msg.getExtension(proto.jspb.test.extendPackedRepeatedBoolList),
414 [true]);
415 assertElementsEquals(
416 msg.getExtension(proto.jspb.test.extendPackedRepeatedForeignEnumList),
417 [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700418
Feng Xiaoe841bac2015-12-11 17:09:20 -0800419}
420
421
422describe('protoBinaryTest', function() {
423 /**
424 * Tests a basic serialization-deserializaton round-trip with all supported
425 * field types (on the TestAllTypes message type).
426 */
427 it('testRoundTrip', function() {
428 var msg = new proto.jspb.test.TestAllTypes();
429 fillAllFields(msg);
430 var encoded = msg.serializeBinary();
431 var decoded = proto.jspb.test.TestAllTypes.deserializeBinary(encoded);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700432 checkAllFields(msg, decoded);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800433 });
434
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700435 /**
436 * Test that base64 string and Uint8Array are interchangeable in bytes fields.
437 */
438 it('testBytesFieldsGettersInterop', function() {
439 var msg = new proto.jspb.test.TestAllTypes();
440 // Set from a base64 string and check all the getters work.
441 msg.setOptionalBytes(BYTES_B64);
442 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
443 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
444 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
445
446 // Test binary serialize round trip doesn't break it.
447 msg = proto.jspb.test.TestAllTypes.deserializeBinary(msg.serializeBinary());
448 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
449 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
450 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
451
452 msg = new proto.jspb.test.TestAllTypes();
453 // Set from a Uint8Array and check all the getters work.
454 msg.setOptionalBytes(BYTES);
455 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
456 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
457 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
458
459 });
460
461 /**
462 * Test that bytes setters will receive result of any of the getters.
463 */
464 it('testBytesFieldsSettersInterop', function() {
465 var msg = new proto.jspb.test.TestAllTypes();
466 msg.setOptionalBytes(BYTES);
467 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
468
469 msg.setOptionalBytes(msg.getOptionalBytes());
470 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
471 msg.setOptionalBytes(msg.getOptionalBytes_asB64());
472 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
473 msg.setOptionalBytes(msg.getOptionalBytes_asU8());
474 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
475 });
476
477 /**
478 * Test that bytes setters will receive result of any of the getters.
479 */
480 it('testRepeatedBytesGetters', function() {
481 var msg = new proto.jspb.test.TestAllTypes();
482
483 function assertGetters() {
Rafi Kamal58d44202019-11-11 17:06:56 -0800484 assertTrue(typeof msg.getRepeatedBytesList_asB64()[0] === 'string');
485 assertTrue(typeof msg.getRepeatedBytesList_asB64()[1] === 'string');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700486 assertTrue(msg.getRepeatedBytesList_asU8()[0] instanceof Uint8Array);
487 assertTrue(msg.getRepeatedBytesList_asU8()[1] instanceof Uint8Array);
488
489 assertTrue(bytesCompare(msg.getRepeatedBytesList()[0], BYTES));
490 assertTrue(bytesCompare(msg.getRepeatedBytesList()[1], BYTES));
491 assertTrue(bytesCompare(msg.getRepeatedBytesList_asB64()[0], BYTES));
492 assertTrue(bytesCompare(msg.getRepeatedBytesList_asB64()[1], BYTES));
493 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[0], BYTES));
494 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[1], BYTES));
495 }
496
497 msg.setRepeatedBytesList([BYTES, BYTES]);
498 assertGetters();
499
500 msg.setRepeatedBytesList([BYTES_B64, BYTES_B64]);
501 assertGetters();
502
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700503 msg.setRepeatedBytesList([]);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700504 assertEquals(0, msg.getRepeatedBytesList().length);
505 assertEquals(0, msg.getRepeatedBytesList_asB64().length);
506 assertEquals(0, msg.getRepeatedBytesList_asU8().length);
507 });
Feng Xiaoe841bac2015-12-11 17:09:20 -0800508
509 /**
510 * Helper: fill all extension values.
511 * @param {proto.jspb.test.TestExtendable} msg
512 */
513 function fillExtensions(msg) {
Adam Cozzette651ba622017-03-06 15:12:19 -0800514 msg.setExtension(proto.jspb.test.extendOptionalInt32, 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800515 msg.setExtension(
516 proto.jspb.test.extendOptionalInt64, -0x7fffffff00000000);
517 msg.setExtension(
518 proto.jspb.test.extendOptionalUint32, 0x80000000);
519 msg.setExtension(
520 proto.jspb.test.extendOptionalUint64, 0xf000000000000000);
521 msg.setExtension(
522 proto.jspb.test.extendOptionalSint32, -100);
523 msg.setExtension(
524 proto.jspb.test.extendOptionalSint64, -0x8000000000000000);
525 msg.setExtension(
526 proto.jspb.test.extendOptionalFixed32, 1234);
527 msg.setExtension(
528 proto.jspb.test.extendOptionalFixed64, 0x1234567800000000);
529 msg.setExtension(
530 proto.jspb.test.extendOptionalSfixed32, -1234);
531 msg.setExtension(
532 proto.jspb.test.extendOptionalSfixed64, -0x1234567800000000);
533 msg.setExtension(
534 proto.jspb.test.extendOptionalFloat, 1.5);
535 msg.setExtension(
536 proto.jspb.test.extendOptionalDouble, -1.5);
537 msg.setExtension(
538 proto.jspb.test.extendOptionalBool, true);
539 msg.setExtension(
540 proto.jspb.test.extendOptionalString, 'hello world');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700541 msg.setExtension(proto.jspb.test.extendOptionalBytes, BYTES);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800542 var submsg = new proto.jspb.test.ExtendsWithMessage();
543 submsg.setFoo(16);
544 msg.setExtension(
545 proto.jspb.test.ExtendsWithMessage.optionalExtension, submsg);
546 msg.setExtension(
547 proto.jspb.test.extendOptionalForeignEnum,
548 proto.jspb.test.ForeignEnum.FOREIGN_FOO);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800549
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700550
Feng Xiaoe841bac2015-12-11 17:09:20 -0800551 msg.setExtension(
552 proto.jspb.test.extendRepeatedInt32List, [-42]);
553 msg.setExtension(
554 proto.jspb.test.extendRepeatedInt64List, [-0x7fffffff00000000]);
555 msg.setExtension(
556 proto.jspb.test.extendRepeatedUint32List, [0x80000000]);
557 msg.setExtension(
558 proto.jspb.test.extendRepeatedUint64List, [0xf000000000000000]);
559 msg.setExtension(
560 proto.jspb.test.extendRepeatedSint32List, [-100]);
561 msg.setExtension(
562 proto.jspb.test.extendRepeatedSint64List, [-0x8000000000000000]);
563 msg.setExtension(
564 proto.jspb.test.extendRepeatedFixed32List, [1234]);
565 msg.setExtension(
566 proto.jspb.test.extendRepeatedFixed64List, [0x1234567800000000]);
567 msg.setExtension(
568 proto.jspb.test.extendRepeatedSfixed32List, [-1234]);
569 msg.setExtension(
570 proto.jspb.test.extendRepeatedSfixed64List, [-0x1234567800000000]);
571 msg.setExtension(
572 proto.jspb.test.extendRepeatedFloatList, [1.5]);
573 msg.setExtension(
574 proto.jspb.test.extendRepeatedDoubleList, [-1.5]);
575 msg.setExtension(
576 proto.jspb.test.extendRepeatedBoolList, [true]);
577 msg.setExtension(
578 proto.jspb.test.extendRepeatedStringList, ['hello world']);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700579 msg.setExtension(proto.jspb.test.extendRepeatedBytesList, [BYTES]);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800580 submsg = new proto.jspb.test.ExtendsWithMessage();
581 submsg.setFoo(1000);
582 msg.setExtension(
583 proto.jspb.test.ExtendsWithMessage.repeatedExtensionList, [submsg]);
584 msg.setExtension(proto.jspb.test.extendRepeatedForeignEnumList,
585 [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
586
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700587
Feng Xiaoe841bac2015-12-11 17:09:20 -0800588 msg.setExtension(
Feng Xiaoe841bac2015-12-11 17:09:20 -0800589 proto.jspb.test.extendPackedRepeatedInt32List, [-42]);
590 msg.setExtension(
591 proto.jspb.test.extendPackedRepeatedInt64List, [-0x7fffffff00000000]);
592 msg.setExtension(
593 proto.jspb.test.extendPackedRepeatedUint32List, [0x80000000]);
594 msg.setExtension(
595 proto.jspb.test.extendPackedRepeatedUint64List, [0xf000000000000000]);
596 msg.setExtension(
597 proto.jspb.test.extendPackedRepeatedSint32List, [-100]);
598 msg.setExtension(
599 proto.jspb.test.extendPackedRepeatedSint64List, [-0x8000000000000000]);
600 msg.setExtension(
601 proto.jspb.test.extendPackedRepeatedFixed32List, [1234]);
602 msg.setExtension(
603 proto.jspb.test.extendPackedRepeatedFixed64List, [0x1234567800000000]);
604 msg.setExtension(
605 proto.jspb.test.extendPackedRepeatedSfixed32List, [-1234]);
606 msg.setExtension(
607 proto.jspb.test.extendPackedRepeatedSfixed64List,
608 [-0x1234567800000000]);
609 msg.setExtension(
610 proto.jspb.test.extendPackedRepeatedFloatList, [1.5]);
611 msg.setExtension(
612 proto.jspb.test.extendPackedRepeatedDoubleList, [-1.5]);
613 msg.setExtension(
614 proto.jspb.test.extendPackedRepeatedBoolList, [true]);
615 msg.setExtension(proto.jspb.test.extendPackedRepeatedForeignEnumList,
616 [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700617
Feng Xiaoe841bac2015-12-11 17:09:20 -0800618 }
619
620
621 /**
622 * Tests extension serialization and deserialization.
623 */
624 it('testExtensions', function() {
625 var msg = new proto.jspb.test.TestExtendable();
626 fillExtensions(msg);
627 var encoded = msg.serializeBinary();
628 var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded);
629 checkExtensions(decoded);
630 });
Adam Cozzette5a76e632016-11-17 16:48:38 -0800631
632 /**
633 * Tests that unknown extensions don't cause deserialization failure.
634 */
635 it('testUnknownExtension', function() {
636 var msg = new proto.jspb.test.TestExtendable();
637 fillExtensions(msg);
638 var writer = new jspb.BinaryWriter();
639 writer.writeBool((1 << 29) - 1, true);
640 proto.jspb.test.TestExtendable.serializeBinaryToWriter(msg, writer);
641 var encoded = writer.getResultBuffer();
642 var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded);
643 checkExtensions(decoded);
644 });
645
646 it('testAnyWellKnownType', function() {
647 var any = new proto.google.protobuf.Any();
648 var msg = new proto.jspb.test.TestAllTypes();
649
650 fillAllFields(msg);
651
652 any.pack(msg.serializeBinary(), 'jspb.test.TestAllTypes');
653
654 assertEquals('type.googleapis.com/jspb.test.TestAllTypes',
655 any.getTypeUrl());
656
657 var msg2 = any.unpack(
658 proto.jspb.test.TestAllTypes.deserializeBinary,
659 'jspb.test.TestAllTypes');
660
661 checkAllFields(msg, msg2);
Adam Cozzette0894e072018-11-09 11:28:22 -0800662
Adam Cozzette5a76e632016-11-17 16:48:38 -0800663 });
Yilun Chong0adb74c2019-01-08 15:06:30 -0800664
Feng Xiaoe841bac2015-12-11 17:09:20 -0800665});