• 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.Message');
36
37// CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test
38goog.require('proto.jspb.test.ExtendsWithMessage');
39goog.require('proto.jspb.test.ForeignEnum');
40goog.require('proto.jspb.test.ForeignMessage');
41goog.require('proto.jspb.test.TestAllTypes');
42goog.require('proto.jspb.test.TestExtendable');
43goog.require('proto.jspb.test.extendOptionalBool');
44goog.require('proto.jspb.test.extendOptionalBytes');
45goog.require('proto.jspb.test.extendOptionalDouble');
46goog.require('proto.jspb.test.extendOptionalFixed32');
47goog.require('proto.jspb.test.extendOptionalFixed64');
48goog.require('proto.jspb.test.extendOptionalFloat');
49goog.require('proto.jspb.test.extendOptionalForeignEnum');
50goog.require('proto.jspb.test.extendOptionalInt32');
51goog.require('proto.jspb.test.extendOptionalInt64');
52goog.require('proto.jspb.test.extendOptionalSfixed32');
53goog.require('proto.jspb.test.extendOptionalSfixed64');
54goog.require('proto.jspb.test.extendOptionalSint32');
55goog.require('proto.jspb.test.extendOptionalSint64');
56goog.require('proto.jspb.test.extendOptionalString');
57goog.require('proto.jspb.test.extendOptionalUint32');
58goog.require('proto.jspb.test.extendOptionalUint64');
59goog.require('proto.jspb.test.extendPackedRepeatedBoolList');
60goog.require('proto.jspb.test.extendPackedRepeatedDoubleList');
61goog.require('proto.jspb.test.extendPackedRepeatedFixed32List');
62goog.require('proto.jspb.test.extendPackedRepeatedFixed64List');
63goog.require('proto.jspb.test.extendPackedRepeatedFloatList');
64goog.require('proto.jspb.test.extendPackedRepeatedForeignEnumList');
65goog.require('proto.jspb.test.extendPackedRepeatedInt32List');
66goog.require('proto.jspb.test.extendPackedRepeatedInt64List');
67goog.require('proto.jspb.test.extendPackedRepeatedSfixed32List');
68goog.require('proto.jspb.test.extendPackedRepeatedSfixed64List');
69goog.require('proto.jspb.test.extendPackedRepeatedSint32List');
70goog.require('proto.jspb.test.extendPackedRepeatedSint64List');
71goog.require('proto.jspb.test.extendPackedRepeatedUint32List');
72goog.require('proto.jspb.test.extendPackedRepeatedUint64List');
73goog.require('proto.jspb.test.extendRepeatedBoolList');
74goog.require('proto.jspb.test.extendRepeatedBytesList');
75goog.require('proto.jspb.test.extendRepeatedDoubleList');
76goog.require('proto.jspb.test.extendRepeatedFixed32List');
77goog.require('proto.jspb.test.extendRepeatedFixed64List');
78goog.require('proto.jspb.test.extendRepeatedFloatList');
79goog.require('proto.jspb.test.extendRepeatedForeignEnumList');
80goog.require('proto.jspb.test.extendRepeatedInt32List');
81goog.require('proto.jspb.test.extendRepeatedInt64List');
82goog.require('proto.jspb.test.extendRepeatedSfixed32List');
83goog.require('proto.jspb.test.extendRepeatedSfixed64List');
84goog.require('proto.jspb.test.extendRepeatedSint32List');
85goog.require('proto.jspb.test.extendRepeatedSint64List');
86goog.require('proto.jspb.test.extendRepeatedStringList');
87goog.require('proto.jspb.test.extendRepeatedUint32List');
88goog.require('proto.jspb.test.extendRepeatedUint64List');
89
90
91var suite = {};
92
93var BYTES = new Uint8Array([1, 2, 8, 9]);
94
95var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES);
96
97
98/**
99 * Helper: fill all fields on a TestAllTypes message.
100 * @param {proto.jspb.test.TestAllTypes} msg
101 */
102function fillAllFields(msg) {
103  msg.setOptionalInt32(-42);
104  // can be exactly represented by JS number (64-bit double, i.e., 52-bit
105  // mantissa).
106  msg.setOptionalInt64(-0x7fffffff00000000);
107  msg.setOptionalUint32(0x80000000);
108  msg.setOptionalUint64(0xf000000000000000);
109  msg.setOptionalSint32(-100);
110  msg.setOptionalSint64(-0x8000000000000000);
111  msg.setOptionalFixed32(1234);
112  msg.setOptionalFixed64(0x1234567800000000);
113  msg.setOptionalSfixed32(-1234);
114  msg.setOptionalSfixed64(-0x1234567800000000);
115  msg.setOptionalFloat(1.5);
116  msg.setOptionalDouble(-1.5);
117  msg.setOptionalBool(true);
118  msg.setOptionalString('hello world');
119  msg.setOptionalBytes(BYTES);
120  msg.setOptionalGroup(new proto.jspb.test.TestAllTypes.OptionalGroup());
121  msg.getOptionalGroup().setA(100);
122  var submsg = new proto.jspb.test.ForeignMessage();
123  submsg.setC(16);
124  msg.setOptionalForeignMessage(submsg);
125  msg.setOptionalForeignEnum(proto.jspb.test.ForeignEnum.FOREIGN_FOO);
126  msg.setOneofString('oneof');
127
128
129  msg.setRepeatedInt32List([-42]);
130  msg.setRepeatedInt64List([-0x7fffffff00000000]);
131  msg.setRepeatedUint32List([0x80000000]);
132  msg.setRepeatedUint64List([0xf000000000000000]);
133  msg.setRepeatedSint32List([-100]);
134  msg.setRepeatedSint64List([-0x8000000000000000]);
135  msg.setRepeatedFixed32List([1234]);
136  msg.setRepeatedFixed64List([0x1234567800000000]);
137  msg.setRepeatedSfixed32List([-1234]);
138  msg.setRepeatedSfixed64List([-0x1234567800000000]);
139  msg.setRepeatedFloatList([1.5]);
140  msg.setRepeatedDoubleList([-1.5]);
141  msg.setRepeatedBoolList([true]);
142  msg.setRepeatedStringList(['hello world']);
143  msg.setRepeatedBytesList([BYTES, BYTES]);
144  msg.setRepeatedGroupList([new proto.jspb.test.TestAllTypes.RepeatedGroup()]);
145  msg.getRepeatedGroupList()[0].setA(100);
146  submsg = new proto.jspb.test.ForeignMessage();
147  submsg.setC(1000);
148  msg.setRepeatedForeignMessageList([submsg]);
149  msg.setRepeatedForeignEnumList([proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
150
151  msg.setPackedRepeatedInt32List([-42]);
152  msg.setPackedRepeatedInt64List([-0x7fffffff00000000]);
153  msg.setPackedRepeatedUint32List([0x80000000]);
154  msg.setPackedRepeatedUint64List([0xf000000000000000]);
155  msg.setPackedRepeatedSint32List([-100]);
156  msg.setPackedRepeatedSint64List([-0x8000000000000000]);
157  msg.setPackedRepeatedFixed32List([1234]);
158  msg.setPackedRepeatedFixed64List([0x1234567800000000]);
159  msg.setPackedRepeatedSfixed32List([-1234]);
160  msg.setPackedRepeatedSfixed64List([-0x1234567800000000]);
161  msg.setPackedRepeatedFloatList([1.5]);
162  msg.setPackedRepeatedDoubleList([-1.5]);
163  msg.setPackedRepeatedBoolList([true]);
164
165}
166
167
168/**
169 * Helper: compare a bytes field to an expected value
170 * @param {Uint8Array|string} arr
171 * @param {Uint8Array} expected
172 * @return {boolean}
173 */
174function bytesCompare(arr, expected) {
175  if (typeof arr === 'string') {
176    arr = goog.crypt.base64.decodeStringToUint8Array(arr);
177  }
178  if (arr.length != expected.length) {
179    return false;
180  }
181  for (var i = 0; i < arr.length; i++) {
182    if (arr[i] != expected[i]) {
183      return false;
184    }
185  }
186  return true;
187}
188
189
190/**
191 * Helper: verify contents of given TestAllTypes message as set by
192 * fillAllFields().
193 * @param {proto.jspb.test.TestAllTypes} original
194 * @param {proto.jspb.test.TestAllTypes} copy
195 */
196function checkAllFields(original, copy) {
197  assertTrue(jspb.Message.equals(original, copy));
198
199  assertEquals(copy.getOptionalInt32(), -42);
200  assertEquals(copy.getOptionalInt64(), -0x7fffffff00000000);
201  assertEquals(copy.getOptionalUint32(), 0x80000000);
202  assertEquals(copy.getOptionalUint64(), 0xf000000000000000);
203  assertEquals(copy.getOptionalSint32(), -100);
204  assertEquals(copy.getOptionalSint64(), -0x8000000000000000);
205  assertEquals(copy.getOptionalFixed32(), 1234);
206  assertEquals(copy.getOptionalFixed64(), 0x1234567800000000);
207  assertEquals(copy.getOptionalSfixed32(), -1234);
208  assertEquals(copy.getOptionalSfixed64(), -0x1234567800000000);
209  assertEquals(copy.getOptionalFloat(), 1.5);
210  assertEquals(copy.getOptionalDouble(), -1.5);
211  assertEquals(copy.getOptionalBool(), true);
212  assertEquals(copy.getOptionalString(), 'hello world');
213  assertEquals(true, bytesCompare(copy.getOptionalBytes(), BYTES));
214  assertEquals(true, bytesCompare(copy.getOptionalBytes_asU8(), BYTES));
215  assertEquals(
216      copy.getOptionalBytes_asB64(), goog.crypt.base64.encodeByteArray(BYTES));
217
218  assertEquals(copy.getOptionalGroup().getA(), 100);
219  assertEquals(copy.getOptionalForeignMessage().getC(), 16);
220  assertEquals(copy.getOptionalForeignEnum(),
221      proto.jspb.test.ForeignEnum.FOREIGN_FOO);
222
223
224  assertEquals(copy.getOneofString(), 'oneof');
225  assertEquals(copy.getOneofFieldCase(),
226      proto.jspb.test.TestAllTypes.OneofFieldCase.ONEOF_STRING);
227
228  assertElementsEquals(copy.getRepeatedInt32List(), [-42]);
229  assertElementsEquals(copy.getRepeatedInt64List(), [-0x7fffffff00000000]);
230  assertElementsEquals(copy.getRepeatedUint32List(), [0x80000000]);
231  assertElementsEquals(copy.getRepeatedUint64List(), [0xf000000000000000]);
232  assertElementsEquals(copy.getRepeatedSint32List(), [-100]);
233  assertElementsEquals(copy.getRepeatedSint64List(), [-0x8000000000000000]);
234  assertElementsEquals(copy.getRepeatedFixed32List(), [1234]);
235  assertElementsEquals(copy.getRepeatedFixed64List(), [0x1234567800000000]);
236  assertElementsEquals(copy.getRepeatedSfixed32List(), [-1234]);
237  assertElementsEquals(copy.getRepeatedSfixed64List(), [-0x1234567800000000]);
238  assertElementsEquals(copy.getRepeatedFloatList(), [1.5]);
239  assertElementsEquals(copy.getRepeatedDoubleList(), [-1.5]);
240  assertElementsEquals(copy.getRepeatedBoolList(), [true]);
241  assertElementsEquals(copy.getRepeatedStringList(), ['hello world']);
242  assertEquals(copy.getRepeatedBytesList().length, 2);
243  assertEquals(true, bytesCompare(copy.getRepeatedBytesList_asU8()[0], BYTES));
244  assertEquals(true, bytesCompare(copy.getRepeatedBytesList()[0], BYTES));
245  assertEquals(true, bytesCompare(copy.getRepeatedBytesList_asU8()[1], BYTES));
246  assertEquals(copy.getRepeatedBytesList_asB64()[0], BYTES_B64);
247  assertEquals(copy.getRepeatedBytesList_asB64()[1], BYTES_B64);
248  assertEquals(copy.getRepeatedGroupList().length, 1);
249  assertEquals(copy.getRepeatedGroupList()[0].getA(), 100);
250  assertEquals(copy.getRepeatedForeignMessageList().length, 1);
251  assertEquals(copy.getRepeatedForeignMessageList()[0].getC(), 1000);
252  assertElementsEquals(copy.getRepeatedForeignEnumList(),
253      [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
254
255  assertElementsEquals(copy.getPackedRepeatedInt32List(), [-42]);
256  assertElementsEquals(copy.getPackedRepeatedInt64List(),
257      [-0x7fffffff00000000]);
258  assertElementsEquals(copy.getPackedRepeatedUint32List(), [0x80000000]);
259  assertElementsEquals(copy.getPackedRepeatedUint64List(),
260      [0xf000000000000000]);
261  assertElementsEquals(copy.getPackedRepeatedSint32List(), [-100]);
262  assertElementsEquals(copy.getPackedRepeatedSint64List(),
263      [-0x8000000000000000]);
264  assertElementsEquals(copy.getPackedRepeatedFixed32List(), [1234]);
265  assertElementsEquals(copy.getPackedRepeatedFixed64List(),
266      [0x1234567800000000]);
267  assertElementsEquals(copy.getPackedRepeatedSfixed32List(), [-1234]);
268  assertElementsEquals(copy.getPackedRepeatedSfixed64List(),
269      [-0x1234567800000000]);
270  assertElementsEquals(copy.getPackedRepeatedFloatList(), [1.5]);
271  assertElementsEquals(copy.getPackedRepeatedDoubleList(), [-1.5]);
272
273}
274
275
276/**
277 * Helper: verify that all expected extensions are present.
278 * @param {!proto.jspb.test.TestExtendable} msg
279 */
280function checkExtensions(msg) {
281  assertEquals(-42,
282      msg.getExtension(proto.jspb.test.extendOptionalInt32));
283  assertEquals(-0x7fffffff00000000,
284      msg.getExtension(proto.jspb.test.extendOptionalInt64));
285  assertEquals(0x80000000,
286      msg.getExtension(proto.jspb.test.extendOptionalUint32));
287  assertEquals(0xf000000000000000,
288      msg.getExtension(proto.jspb.test.extendOptionalUint64));
289  assertEquals(-100,
290      msg.getExtension(proto.jspb.test.extendOptionalSint32));
291  assertEquals(-0x8000000000000000,
292      msg.getExtension(proto.jspb.test.extendOptionalSint64));
293  assertEquals(1234,
294      msg.getExtension(proto.jspb.test.extendOptionalFixed32));
295  assertEquals(0x1234567800000000,
296      msg.getExtension(proto.jspb.test.extendOptionalFixed64));
297  assertEquals(-1234,
298      msg.getExtension(proto.jspb.test.extendOptionalSfixed32));
299  assertEquals(-0x1234567800000000,
300      msg.getExtension(proto.jspb.test.extendOptionalSfixed64));
301  assertEquals(1.5,
302      msg.getExtension(proto.jspb.test.extendOptionalFloat));
303  assertEquals(-1.5,
304      msg.getExtension(proto.jspb.test.extendOptionalDouble));
305  assertEquals(true,
306      msg.getExtension(proto.jspb.test.extendOptionalBool));
307  assertEquals('hello world',
308      msg.getExtension(proto.jspb.test.extendOptionalString));
309  assertEquals(
310      true, bytesCompare(
311                msg.getExtension(proto.jspb.test.extendOptionalBytes), BYTES));
312  assertEquals(16,
313      msg.getExtension(
314          proto.jspb.test.ExtendsWithMessage.optionalExtension).getFoo());
315
316
317  assertElementsEquals(
318      msg.getExtension(proto.jspb.test.extendRepeatedInt32List),
319      [-42]);
320  assertElementsEquals(
321      msg.getExtension(proto.jspb.test.extendRepeatedInt64List),
322      [-0x7fffffff00000000]);
323  assertElementsEquals(
324      msg.getExtension(proto.jspb.test.extendRepeatedUint32List),
325      [0x80000000]);
326  assertElementsEquals(
327      msg.getExtension(proto.jspb.test.extendRepeatedUint64List),
328      [0xf000000000000000]);
329  assertElementsEquals(
330      msg.getExtension(proto.jspb.test.extendRepeatedSint32List),
331      [-100]);
332  assertElementsEquals(
333      msg.getExtension(proto.jspb.test.extendRepeatedSint64List),
334      [-0x8000000000000000]);
335  assertElementsEquals(
336      msg.getExtension(proto.jspb.test.extendRepeatedFixed32List),
337      [1234]);
338  assertElementsEquals(
339      msg.getExtension(proto.jspb.test.extendRepeatedFixed64List),
340      [0x1234567800000000]);
341  assertElementsEquals(
342      msg.getExtension(proto.jspb.test.extendRepeatedSfixed32List),
343      [-1234]);
344  assertElementsEquals(
345      msg.getExtension(proto.jspb.test.extendRepeatedSfixed64List),
346      [-0x1234567800000000]);
347  assertElementsEquals(
348      msg.getExtension(proto.jspb.test.extendRepeatedFloatList),
349      [1.5]);
350  assertElementsEquals(
351      msg.getExtension(proto.jspb.test.extendRepeatedDoubleList),
352      [-1.5]);
353  assertElementsEquals(
354      msg.getExtension(proto.jspb.test.extendRepeatedBoolList),
355      [true]);
356  assertElementsEquals(
357      msg.getExtension(proto.jspb.test.extendRepeatedStringList),
358      ['hello world']);
359  assertEquals(
360      true,
361      bytesCompare(
362          msg.getExtension(proto.jspb.test.extendRepeatedBytesList)[0], BYTES));
363  assertEquals(1000,
364      msg.getExtension(
365          proto.jspb.test.ExtendsWithMessage.repeatedExtensionList)[0]
366      .getFoo());
367  assertElementsEquals(
368      msg.getExtension(proto.jspb.test.extendRepeatedForeignEnumList),
369      [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
370
371
372  assertElementsEquals(
373      msg.getExtension(proto.jspb.test.extendPackedRepeatedInt32List),
374      [-42]);
375  assertElementsEquals(
376      msg.getExtension(proto.jspb.test.extendPackedRepeatedInt64List),
377      [-0x7fffffff00000000]);
378  assertElementsEquals(
379      msg.getExtension(proto.jspb.test.extendPackedRepeatedUint32List),
380      [0x80000000]);
381  assertElementsEquals(
382      msg.getExtension(proto.jspb.test.extendPackedRepeatedUint64List),
383      [0xf000000000000000]);
384  assertElementsEquals(
385      msg.getExtension(proto.jspb.test.extendPackedRepeatedSint32List),
386      [-100]);
387  assertElementsEquals(
388      msg.getExtension(proto.jspb.test.extendPackedRepeatedSint64List),
389      [-0x8000000000000000]);
390  assertElementsEquals(
391      msg.getExtension(proto.jspb.test.extendPackedRepeatedFixed32List),
392      [1234]);
393  assertElementsEquals(
394      msg.getExtension(proto.jspb.test.extendPackedRepeatedFixed64List),
395      [0x1234567800000000]);
396  assertElementsEquals(
397      msg.getExtension(proto.jspb.test.extendPackedRepeatedSfixed32List),
398      [-1234]);
399  assertElementsEquals(
400      msg.getExtension(proto.jspb.test.extendPackedRepeatedSfixed64List),
401      [-0x1234567800000000]);
402  assertElementsEquals(
403      msg.getExtension(proto.jspb.test.extendPackedRepeatedFloatList),
404      [1.5]);
405  assertElementsEquals(
406      msg.getExtension(proto.jspb.test.extendPackedRepeatedDoubleList),
407      [-1.5]);
408  assertElementsEquals(
409      msg.getExtension(proto.jspb.test.extendPackedRepeatedBoolList),
410      [true]);
411  assertElementsEquals(
412      msg.getExtension(proto.jspb.test.extendPackedRepeatedForeignEnumList),
413      [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
414
415}
416
417
418describe('protoBinaryTest', function() {
419  /**
420   * Tests a basic serialization-deserializaton round-trip with all supported
421   * field types (on the TestAllTypes message type).
422   */
423  it('testRoundTrip', function() {
424    var msg = new proto.jspb.test.TestAllTypes();
425    fillAllFields(msg);
426    var encoded = msg.serializeBinary();
427    var decoded = proto.jspb.test.TestAllTypes.deserializeBinary(encoded);
428    checkAllFields(msg, decoded);
429  });
430
431  /**
432   * Test that base64 string and Uint8Array are interchangeable in bytes fields.
433   */
434  it('testBytesFieldsGettersInterop', function() {
435    var msg = new proto.jspb.test.TestAllTypes();
436    // Set from a base64 string and check all the getters work.
437    msg.setOptionalBytes(BYTES_B64);
438    assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
439    assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
440    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
441
442    // Test binary serialize round trip doesn't break it.
443    msg = proto.jspb.test.TestAllTypes.deserializeBinary(msg.serializeBinary());
444    assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
445    assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
446    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
447
448    msg = new proto.jspb.test.TestAllTypes();
449    // Set from a Uint8Array and check all the getters work.
450    msg.setOptionalBytes(BYTES);
451    assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
452    assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
453    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
454
455  });
456
457  /**
458   * Test that bytes setters will receive result of any of the getters.
459   */
460  it('testBytesFieldsSettersInterop', function() {
461    var msg = new proto.jspb.test.TestAllTypes();
462    msg.setOptionalBytes(BYTES);
463    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
464
465    msg.setOptionalBytes(msg.getOptionalBytes());
466    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
467    msg.setOptionalBytes(msg.getOptionalBytes_asB64());
468    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
469    msg.setOptionalBytes(msg.getOptionalBytes_asU8());
470    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
471  });
472
473  /**
474   * Test that bytes setters will receive result of any of the getters.
475   */
476  it('testRepeatedBytesGetters', function() {
477    var msg = new proto.jspb.test.TestAllTypes();
478
479    function assertGetters() {
480      assertTrue(typeof msg.getRepeatedBytesList_asB64()[0] === 'string');
481      assertTrue(typeof msg.getRepeatedBytesList_asB64()[1] === 'string');
482      assertTrue(msg.getRepeatedBytesList_asU8()[0] instanceof Uint8Array);
483      assertTrue(msg.getRepeatedBytesList_asU8()[1] instanceof Uint8Array);
484
485      assertTrue(bytesCompare(msg.getRepeatedBytesList()[0], BYTES));
486      assertTrue(bytesCompare(msg.getRepeatedBytesList()[1], BYTES));
487      assertTrue(bytesCompare(msg.getRepeatedBytesList_asB64()[0], BYTES));
488      assertTrue(bytesCompare(msg.getRepeatedBytesList_asB64()[1], BYTES));
489      assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[0], BYTES));
490      assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[1], BYTES));
491    }
492
493    msg.setRepeatedBytesList([BYTES, BYTES]);
494    assertGetters();
495
496    msg.setRepeatedBytesList([BYTES_B64, BYTES_B64]);
497    assertGetters();
498
499    msg.setRepeatedBytesList(null);
500    assertEquals(0, msg.getRepeatedBytesList().length);
501    assertEquals(0, msg.getRepeatedBytesList_asB64().length);
502    assertEquals(0, msg.getRepeatedBytesList_asU8().length);
503  });
504
505  /**
506   * Helper: fill all extension values.
507   * @param {proto.jspb.test.TestExtendable} msg
508   */
509  function fillExtensions(msg) {
510    msg.setExtension(
511        proto.jspb.test.extendOptionalInt32, -42);
512    msg.setExtension(
513        proto.jspb.test.extendOptionalInt64, -0x7fffffff00000000);
514    msg.setExtension(
515        proto.jspb.test.extendOptionalUint32, 0x80000000);
516    msg.setExtension(
517        proto.jspb.test.extendOptionalUint64, 0xf000000000000000);
518    msg.setExtension(
519        proto.jspb.test.extendOptionalSint32, -100);
520    msg.setExtension(
521        proto.jspb.test.extendOptionalSint64, -0x8000000000000000);
522    msg.setExtension(
523        proto.jspb.test.extendOptionalFixed32, 1234);
524    msg.setExtension(
525        proto.jspb.test.extendOptionalFixed64, 0x1234567800000000);
526    msg.setExtension(
527        proto.jspb.test.extendOptionalSfixed32, -1234);
528    msg.setExtension(
529        proto.jspb.test.extendOptionalSfixed64, -0x1234567800000000);
530    msg.setExtension(
531        proto.jspb.test.extendOptionalFloat, 1.5);
532    msg.setExtension(
533        proto.jspb.test.extendOptionalDouble, -1.5);
534    msg.setExtension(
535        proto.jspb.test.extendOptionalBool, true);
536    msg.setExtension(
537        proto.jspb.test.extendOptionalString, 'hello world');
538    msg.setExtension(proto.jspb.test.extendOptionalBytes, BYTES);
539    var submsg = new proto.jspb.test.ExtendsWithMessage();
540    submsg.setFoo(16);
541    msg.setExtension(
542        proto.jspb.test.ExtendsWithMessage.optionalExtension, submsg);
543    msg.setExtension(
544        proto.jspb.test.extendOptionalForeignEnum,
545        proto.jspb.test.ForeignEnum.FOREIGN_FOO);
546
547
548    msg.setExtension(
549        proto.jspb.test.extendRepeatedInt32List, [-42]);
550    msg.setExtension(
551        proto.jspb.test.extendRepeatedInt64List, [-0x7fffffff00000000]);
552    msg.setExtension(
553        proto.jspb.test.extendRepeatedUint32List, [0x80000000]);
554    msg.setExtension(
555        proto.jspb.test.extendRepeatedUint64List, [0xf000000000000000]);
556    msg.setExtension(
557        proto.jspb.test.extendRepeatedSint32List, [-100]);
558    msg.setExtension(
559        proto.jspb.test.extendRepeatedSint64List, [-0x8000000000000000]);
560    msg.setExtension(
561        proto.jspb.test.extendRepeatedFixed32List, [1234]);
562    msg.setExtension(
563        proto.jspb.test.extendRepeatedFixed64List, [0x1234567800000000]);
564    msg.setExtension(
565        proto.jspb.test.extendRepeatedSfixed32List, [-1234]);
566    msg.setExtension(
567        proto.jspb.test.extendRepeatedSfixed64List, [-0x1234567800000000]);
568    msg.setExtension(
569        proto.jspb.test.extendRepeatedFloatList, [1.5]);
570    msg.setExtension(
571        proto.jspb.test.extendRepeatedDoubleList, [-1.5]);
572    msg.setExtension(
573        proto.jspb.test.extendRepeatedBoolList, [true]);
574    msg.setExtension(
575        proto.jspb.test.extendRepeatedStringList, ['hello world']);
576    msg.setExtension(proto.jspb.test.extendRepeatedBytesList, [BYTES]);
577    submsg = new proto.jspb.test.ExtendsWithMessage();
578    submsg.setFoo(1000);
579    msg.setExtension(
580        proto.jspb.test.ExtendsWithMessage.repeatedExtensionList, [submsg]);
581    msg.setExtension(proto.jspb.test.extendRepeatedForeignEnumList,
582        [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
583
584
585    msg.setExtension(
586        proto.jspb.test.extendPackedRepeatedInt32List, [-42]);
587    msg.setExtension(
588        proto.jspb.test.extendPackedRepeatedInt64List, [-0x7fffffff00000000]);
589    msg.setExtension(
590        proto.jspb.test.extendPackedRepeatedUint32List, [0x80000000]);
591    msg.setExtension(
592        proto.jspb.test.extendPackedRepeatedUint64List, [0xf000000000000000]);
593    msg.setExtension(
594        proto.jspb.test.extendPackedRepeatedSint32List, [-100]);
595    msg.setExtension(
596        proto.jspb.test.extendPackedRepeatedSint64List, [-0x8000000000000000]);
597    msg.setExtension(
598        proto.jspb.test.extendPackedRepeatedFixed32List, [1234]);
599    msg.setExtension(
600        proto.jspb.test.extendPackedRepeatedFixed64List, [0x1234567800000000]);
601    msg.setExtension(
602        proto.jspb.test.extendPackedRepeatedSfixed32List, [-1234]);
603    msg.setExtension(
604        proto.jspb.test.extendPackedRepeatedSfixed64List,
605        [-0x1234567800000000]);
606    msg.setExtension(
607        proto.jspb.test.extendPackedRepeatedFloatList, [1.5]);
608    msg.setExtension(
609        proto.jspb.test.extendPackedRepeatedDoubleList, [-1.5]);
610    msg.setExtension(
611        proto.jspb.test.extendPackedRepeatedBoolList, [true]);
612    msg.setExtension(proto.jspb.test.extendPackedRepeatedForeignEnumList,
613        [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
614
615  }
616
617
618  /**
619   * Tests extension serialization and deserialization.
620   */
621  it('testExtensions', function() {
622    var msg = new proto.jspb.test.TestExtendable();
623    fillExtensions(msg);
624    var encoded = msg.serializeBinary();
625    var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded);
626    checkExtensions(decoded);
627  });
628});
629