• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
31// Test suite is written using Jasmine -- see http://jasmine.github.io/
32
33goog.require('goog.crypt.base64');
34goog.require('goog.testing.asserts');
35goog.require('jspb.BinaryWriter');
36goog.require('jspb.Message');
37
38// CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test
39goog.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');
44goog.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
91// CommonJS-LoadFromFile: ../node_modules/google-protobuf/google/protobuf/any_pb proto.google.protobuf
92goog.require('proto.google.protobuf.Any');
93
94
95var suite = {};
96
97var BYTES = new Uint8Array([1, 2, 8, 9]);
98
99var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES);
100
101
102/**
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');
123  msg.setOptionalBytes(BYTES);
124  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);
130  msg.setOneofString('oneof');
131
132
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, BYTES]);
148  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]);
168
169}
170
171
172/**
173 * Helper: compare a bytes field to an expected value
174 * @param {Uint8Array|string} arr
175 * @param {Uint8Array} expected
176 * @return {boolean}
177 */
178function bytesCompare(arr, expected) {
179  if (typeof arr === 'string') {
180    arr = goog.crypt.base64.decodeStringToUint8Array(arr);
181  }
182  if (arr.length != expected.length) {
183    return false;
184  }
185  for (var i = 0; i < arr.length; i++) {
186    if (arr[i] != expected[i]) {
187      return false;
188    }
189  }
190  return true;
191}
192
193
194/**
195 * Helper: verify contents of given TestAllTypes message as set by
196 * fillAllFields().
197 * @param {proto.jspb.test.TestAllTypes} original
198 * @param {proto.jspb.test.TestAllTypes} copy
199 */
200function checkAllFields(original, copy) {
201  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(),
223      proto.jspb.test.ForeignEnum.FOREIGN_FOO);
224
225
226  assertEquals(copy.getOneofString(), 'oneof');
227  assertEquals(copy.getOneofFieldCase(),
228      proto.jspb.test.TestAllTypes.OneofFieldCase.ONEOF_STRING);
229
230  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(),
255      [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
256
257  assertElementsEquals(copy.getPackedRepeatedInt32List(), [-42]);
258  assertElementsEquals(copy.getPackedRepeatedInt64List(),
259      [-0x7fffffff00000000]);
260  assertElementsEquals(copy.getPackedRepeatedUint32List(), [0x80000000]);
261  assertElementsEquals(copy.getPackedRepeatedUint64List(),
262      [0xf000000000000000]);
263  assertElementsEquals(copy.getPackedRepeatedSint32List(), [-100]);
264  assertElementsEquals(copy.getPackedRepeatedSint64List(),
265      [-0x8000000000000000]);
266  assertElementsEquals(copy.getPackedRepeatedFixed32List(), [1234]);
267  assertElementsEquals(copy.getPackedRepeatedFixed64List(),
268      [0x1234567800000000]);
269  assertElementsEquals(copy.getPackedRepeatedSfixed32List(), [-1234]);
270  assertElementsEquals(copy.getPackedRepeatedSfixed64List(),
271      [-0x1234567800000000]);
272  assertElementsEquals(copy.getPackedRepeatedFloatList(), [1.5]);
273  assertElementsEquals(copy.getPackedRepeatedDoubleList(), [-1.5]);
274
275
276  // Check last so we get more granular errors first.
277  assertTrue(jspb.Message.equals(original, copy));
278}
279
280
281/**
282 * Helper: verify that all expected extensions are present.
283 * @param {!proto.jspb.test.TestExtendable} msg
284 */
285function checkExtensions(msg) {
286  assertEquals(0, msg.getExtension(proto.jspb.test.extendOptionalInt32));
287  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));
313  assertEquals(
314      true, bytesCompare(
315                msg.getExtension(proto.jspb.test.extendOptionalBytes), BYTES));
316  assertEquals(16,
317      msg.getExtension(
318          proto.jspb.test.ExtendsWithMessage.optionalExtension).getFoo());
319
320
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']);
363  assertEquals(
364      true,
365      bytesCompare(
366          msg.getExtension(proto.jspb.test.extendRepeatedBytesList)[0], BYTES));
367  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
375
376  assertElementsEquals(
377      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]);
418
419}
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);
432    checkAllFields(msg, decoded);
433  });
434
435  /**
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() {
484      assertTrue(typeof msg.getRepeatedBytesList_asB64()[0] === 'string');
485      assertTrue(typeof msg.getRepeatedBytesList_asB64()[1] === 'string');
486      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
503    msg.setRepeatedBytesList([]);
504    assertEquals(0, msg.getRepeatedBytesList().length);
505    assertEquals(0, msg.getRepeatedBytesList_asB64().length);
506    assertEquals(0, msg.getRepeatedBytesList_asU8().length);
507  });
508
509  /**
510   * Helper: fill all extension values.
511   * @param {proto.jspb.test.TestExtendable} msg
512   */
513  function fillExtensions(msg) {
514    msg.setExtension(proto.jspb.test.extendOptionalInt32, 0);
515    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');
541    msg.setExtension(proto.jspb.test.extendOptionalBytes, BYTES);
542    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);
549
550
551    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']);
579    msg.setExtension(proto.jspb.test.extendRepeatedBytesList, [BYTES]);
580    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
587
588    msg.setExtension(
589        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]);
617
618  }
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  });
631
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);
662
663  });
664
665});
666