proto_test.js 25.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Test suite is written using Jasmine -- see http://jasmine.github.io/

33
goog.require('goog.crypt.base64');
34
goog.require('goog.testing.asserts');
35
goog.require('jspb.Message');
36

37
// CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test
38 39 40 41 42
goog.require('proto.jspb.test.ExtendsWithMessage');
goog.require('proto.jspb.test.ForeignEnum');
goog.require('proto.jspb.test.ForeignMessage');
goog.require('proto.jspb.test.TestAllTypes');
goog.require('proto.jspb.test.TestExtendable');
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
goog.require('proto.jspb.test.extendOptionalBool');
goog.require('proto.jspb.test.extendOptionalBytes');
goog.require('proto.jspb.test.extendOptionalDouble');
goog.require('proto.jspb.test.extendOptionalFixed32');
goog.require('proto.jspb.test.extendOptionalFixed64');
goog.require('proto.jspb.test.extendOptionalFloat');
goog.require('proto.jspb.test.extendOptionalForeignEnum');
goog.require('proto.jspb.test.extendOptionalInt32');
goog.require('proto.jspb.test.extendOptionalInt64');
goog.require('proto.jspb.test.extendOptionalSfixed32');
goog.require('proto.jspb.test.extendOptionalSfixed64');
goog.require('proto.jspb.test.extendOptionalSint32');
goog.require('proto.jspb.test.extendOptionalSint64');
goog.require('proto.jspb.test.extendOptionalString');
goog.require('proto.jspb.test.extendOptionalUint32');
goog.require('proto.jspb.test.extendOptionalUint64');
goog.require('proto.jspb.test.extendPackedRepeatedBoolList');
goog.require('proto.jspb.test.extendPackedRepeatedDoubleList');
goog.require('proto.jspb.test.extendPackedRepeatedFixed32List');
goog.require('proto.jspb.test.extendPackedRepeatedFixed64List');
goog.require('proto.jspb.test.extendPackedRepeatedFloatList');
goog.require('proto.jspb.test.extendPackedRepeatedForeignEnumList');
goog.require('proto.jspb.test.extendPackedRepeatedInt32List');
goog.require('proto.jspb.test.extendPackedRepeatedInt64List');
goog.require('proto.jspb.test.extendPackedRepeatedSfixed32List');
goog.require('proto.jspb.test.extendPackedRepeatedSfixed64List');
goog.require('proto.jspb.test.extendPackedRepeatedSint32List');
goog.require('proto.jspb.test.extendPackedRepeatedSint64List');
goog.require('proto.jspb.test.extendPackedRepeatedUint32List');
goog.require('proto.jspb.test.extendPackedRepeatedUint64List');
goog.require('proto.jspb.test.extendRepeatedBoolList');
goog.require('proto.jspb.test.extendRepeatedBytesList');
goog.require('proto.jspb.test.extendRepeatedDoubleList');
goog.require('proto.jspb.test.extendRepeatedFixed32List');
goog.require('proto.jspb.test.extendRepeatedFixed64List');
goog.require('proto.jspb.test.extendRepeatedFloatList');
goog.require('proto.jspb.test.extendRepeatedForeignEnumList');
goog.require('proto.jspb.test.extendRepeatedInt32List');
goog.require('proto.jspb.test.extendRepeatedInt64List');
goog.require('proto.jspb.test.extendRepeatedSfixed32List');
goog.require('proto.jspb.test.extendRepeatedSfixed64List');
goog.require('proto.jspb.test.extendRepeatedSint32List');
goog.require('proto.jspb.test.extendRepeatedSint64List');
goog.require('proto.jspb.test.extendRepeatedStringList');
goog.require('proto.jspb.test.extendRepeatedUint32List');
goog.require('proto.jspb.test.extendRepeatedUint64List');

90 91 92

var suite = {};

93 94 95 96 97
var BYTES = new Uint8Array([1, 2, 8, 9]);

var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES);


98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/**
 * Helper: fill all fields on a TestAllTypes message.
 * @param {proto.jspb.test.TestAllTypes} msg
 */
function fillAllFields(msg) {
  msg.setOptionalInt32(-42);
  // can be exactly represented by JS number (64-bit double, i.e., 52-bit
  // mantissa).
  msg.setOptionalInt64(-0x7fffffff00000000);
  msg.setOptionalUint32(0x80000000);
  msg.setOptionalUint64(0xf000000000000000);
  msg.setOptionalSint32(-100);
  msg.setOptionalSint64(-0x8000000000000000);
  msg.setOptionalFixed32(1234);
  msg.setOptionalFixed64(0x1234567800000000);
  msg.setOptionalSfixed32(-1234);
  msg.setOptionalSfixed64(-0x1234567800000000);
  msg.setOptionalFloat(1.5);
  msg.setOptionalDouble(-1.5);
  msg.setOptionalBool(true);
  msg.setOptionalString('hello world');
119
  msg.setOptionalBytes(BYTES);
120 121 122 123 124 125 126 127
  msg.setOptionalGroup(new proto.jspb.test.TestAllTypes.OptionalGroup());
  msg.getOptionalGroup().setA(100);
  var submsg = new proto.jspb.test.ForeignMessage();
  submsg.setC(16);
  msg.setOptionalForeignMessage(submsg);
  msg.setOptionalForeignEnum(proto.jspb.test.ForeignEnum.FOREIGN_FOO);
  msg.setOneofString('oneof');

128

129 130 131 132 133 134 135 136 137 138 139 140 141 142
  msg.setRepeatedInt32List([-42]);
  msg.setRepeatedInt64List([-0x7fffffff00000000]);
  msg.setRepeatedUint32List([0x80000000]);
  msg.setRepeatedUint64List([0xf000000000000000]);
  msg.setRepeatedSint32List([-100]);
  msg.setRepeatedSint64List([-0x8000000000000000]);
  msg.setRepeatedFixed32List([1234]);
  msg.setRepeatedFixed64List([0x1234567800000000]);
  msg.setRepeatedSfixed32List([-1234]);
  msg.setRepeatedSfixed64List([-0x1234567800000000]);
  msg.setRepeatedFloatList([1.5]);
  msg.setRepeatedDoubleList([-1.5]);
  msg.setRepeatedBoolList([true]);
  msg.setRepeatedStringList(['hello world']);
143
  msg.setRepeatedBytesList([BYTES, BYTES]);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  msg.setRepeatedGroupList([new proto.jspb.test.TestAllTypes.RepeatedGroup()]);
  msg.getRepeatedGroupList()[0].setA(100);
  submsg = new proto.jspb.test.ForeignMessage();
  submsg.setC(1000);
  msg.setRepeatedForeignMessageList([submsg]);
  msg.setRepeatedForeignEnumList([proto.jspb.test.ForeignEnum.FOREIGN_FOO]);

  msg.setPackedRepeatedInt32List([-42]);
  msg.setPackedRepeatedInt64List([-0x7fffffff00000000]);
  msg.setPackedRepeatedUint32List([0x80000000]);
  msg.setPackedRepeatedUint64List([0xf000000000000000]);
  msg.setPackedRepeatedSint32List([-100]);
  msg.setPackedRepeatedSint64List([-0x8000000000000000]);
  msg.setPackedRepeatedFixed32List([1234]);
  msg.setPackedRepeatedFixed64List([0x1234567800000000]);
  msg.setPackedRepeatedSfixed32List([-1234]);
  msg.setPackedRepeatedSfixed64List([-0x1234567800000000]);
  msg.setPackedRepeatedFloatList([1.5]);
  msg.setPackedRepeatedDoubleList([-1.5]);
  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 173
 * @return {boolean}
 */
174 175 176 177 178
function bytesCompare(arr, expected) {
  if (goog.isString(arr)) {
    arr = goog.crypt.base64.decodeStringToUint8Array(arr);
  }
  if (arr.length != expected.length) {
179 180
    return false;
  }
181 182 183
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] != expected[i]) {
      return false;
184 185
    }
  }
186
  return true;
187 188 189 190 191 192
}


/**
 * Helper: verify contents of given TestAllTypes message as set by
 * fillAllFields().
193 194
 * @param {proto.jspb.test.TestAllTypes} original
 * @param {proto.jspb.test.TestAllTypes} copy
195
 */
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
function checkAllFields(original, copy) {
  assertTrue(jspb.Message.equals(original, copy));

  assertEquals(copy.getOptionalInt32(), -42);
  assertEquals(copy.getOptionalInt64(), -0x7fffffff00000000);
  assertEquals(copy.getOptionalUint32(), 0x80000000);
  assertEquals(copy.getOptionalUint64(), 0xf000000000000000);
  assertEquals(copy.getOptionalSint32(), -100);
  assertEquals(copy.getOptionalSint64(), -0x8000000000000000);
  assertEquals(copy.getOptionalFixed32(), 1234);
  assertEquals(copy.getOptionalFixed64(), 0x1234567800000000);
  assertEquals(copy.getOptionalSfixed32(), -1234);
  assertEquals(copy.getOptionalSfixed64(), -0x1234567800000000);
  assertEquals(copy.getOptionalFloat(), 1.5);
  assertEquals(copy.getOptionalDouble(), -1.5);
  assertEquals(copy.getOptionalBool(), true);
  assertEquals(copy.getOptionalString(), 'hello world');
  assertEquals(true, bytesCompare(copy.getOptionalBytes(), BYTES));
  assertEquals(true, bytesCompare(copy.getOptionalBytes_asU8(), BYTES));
  assertEquals(
      copy.getOptionalBytes_asB64(), goog.crypt.base64.encodeByteArray(BYTES));

  assertEquals(copy.getOptionalGroup().getA(), 100);
  assertEquals(copy.getOptionalForeignMessage().getC(), 16);
  assertEquals(copy.getOptionalForeignEnum(),
221
      proto.jspb.test.ForeignEnum.FOREIGN_FOO);
222 223 224 225


  assertEquals(copy.getOneofString(), 'oneof');
  assertEquals(copy.getOneofFieldCase(),
226 227
      proto.jspb.test.TestAllTypes.OneofFieldCase.ONEOF_STRING);

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  assertElementsEquals(copy.getRepeatedInt32List(), [-42]);
  assertElementsEquals(copy.getRepeatedInt64List(), [-0x7fffffff00000000]);
  assertElementsEquals(copy.getRepeatedUint32List(), [0x80000000]);
  assertElementsEquals(copy.getRepeatedUint64List(), [0xf000000000000000]);
  assertElementsEquals(copy.getRepeatedSint32List(), [-100]);
  assertElementsEquals(copy.getRepeatedSint64List(), [-0x8000000000000000]);
  assertElementsEquals(copy.getRepeatedFixed32List(), [1234]);
  assertElementsEquals(copy.getRepeatedFixed64List(), [0x1234567800000000]);
  assertElementsEquals(copy.getRepeatedSfixed32List(), [-1234]);
  assertElementsEquals(copy.getRepeatedSfixed64List(), [-0x1234567800000000]);
  assertElementsEquals(copy.getRepeatedFloatList(), [1.5]);
  assertElementsEquals(copy.getRepeatedDoubleList(), [-1.5]);
  assertElementsEquals(copy.getRepeatedBoolList(), [true]);
  assertElementsEquals(copy.getRepeatedStringList(), ['hello world']);
  assertEquals(copy.getRepeatedBytesList().length, 2);
  assertEquals(true, bytesCompare(copy.getRepeatedBytesList_asU8()[0], BYTES));
  assertEquals(true, bytesCompare(copy.getRepeatedBytesList()[0], BYTES));
  assertEquals(true, bytesCompare(copy.getRepeatedBytesList_asU8()[1], BYTES));
  assertEquals(copy.getRepeatedBytesList_asB64()[0], BYTES_B64);
  assertEquals(copy.getRepeatedBytesList_asB64()[1], BYTES_B64);
  assertEquals(copy.getRepeatedGroupList().length, 1);
  assertEquals(copy.getRepeatedGroupList()[0].getA(), 100);
  assertEquals(copy.getRepeatedForeignMessageList().length, 1);
  assertEquals(copy.getRepeatedForeignMessageList()[0].getC(), 1000);
  assertElementsEquals(copy.getRepeatedForeignEnumList(),
253 254
      [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);

255 256
  assertElementsEquals(copy.getPackedRepeatedInt32List(), [-42]);
  assertElementsEquals(copy.getPackedRepeatedInt64List(),
257
      [-0x7fffffff00000000]);
258 259 260 261 262
  assertElementsEquals(copy.getPackedRepeatedUint32List(), [0x80000000]);
  assertElementsEquals(copy.getPackedRepeatedUint64List(),
      [0xf000000000000000]);
  assertElementsEquals(copy.getPackedRepeatedSint32List(), [-100]);
  assertElementsEquals(copy.getPackedRepeatedSint64List(),
263
      [-0x8000000000000000]);
264 265
  assertElementsEquals(copy.getPackedRepeatedFixed32List(), [1234]);
  assertElementsEquals(copy.getPackedRepeatedFixed64List(),
266
      [0x1234567800000000]);
267 268
  assertElementsEquals(copy.getPackedRepeatedSfixed32List(), [-1234]);
  assertElementsEquals(copy.getPackedRepeatedSfixed64List(),
269
      [-0x1234567800000000]);
270 271 272
  assertElementsEquals(copy.getPackedRepeatedFloatList(), [1.5]);
  assertElementsEquals(copy.getPackedRepeatedDoubleList(), [-1.5]);

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
}


/**
 * Helper: verify that all expected extensions are present.
 * @param {!proto.jspb.test.TestExtendable} msg
 */
function checkExtensions(msg) {
  assertEquals(-42,
      msg.getExtension(proto.jspb.test.extendOptionalInt32));
  assertEquals(-0x7fffffff00000000,
      msg.getExtension(proto.jspb.test.extendOptionalInt64));
  assertEquals(0x80000000,
      msg.getExtension(proto.jspb.test.extendOptionalUint32));
  assertEquals(0xf000000000000000,
      msg.getExtension(proto.jspb.test.extendOptionalUint64));
  assertEquals(-100,
      msg.getExtension(proto.jspb.test.extendOptionalSint32));
  assertEquals(-0x8000000000000000,
      msg.getExtension(proto.jspb.test.extendOptionalSint64));
  assertEquals(1234,
      msg.getExtension(proto.jspb.test.extendOptionalFixed32));
  assertEquals(0x1234567800000000,
      msg.getExtension(proto.jspb.test.extendOptionalFixed64));
  assertEquals(-1234,
      msg.getExtension(proto.jspb.test.extendOptionalSfixed32));
  assertEquals(-0x1234567800000000,
      msg.getExtension(proto.jspb.test.extendOptionalSfixed64));
  assertEquals(1.5,
      msg.getExtension(proto.jspb.test.extendOptionalFloat));
  assertEquals(-1.5,
      msg.getExtension(proto.jspb.test.extendOptionalDouble));
  assertEquals(true,
      msg.getExtension(proto.jspb.test.extendOptionalBool));
  assertEquals('hello world',
      msg.getExtension(proto.jspb.test.extendOptionalString));
309 310 311
  assertEquals(
      true, bytesCompare(
                msg.getExtension(proto.jspb.test.extendOptionalBytes), BYTES));
312 313 314
  assertEquals(16,
      msg.getExtension(
          proto.jspb.test.ExtendsWithMessage.optionalExtension).getFoo());
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedInt32List),
      [-42]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedInt64List),
      [-0x7fffffff00000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedUint32List),
      [0x80000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedUint64List),
      [0xf000000000000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedSint32List),
      [-100]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedSint64List),
      [-0x8000000000000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedFixed32List),
      [1234]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedFixed64List),
      [0x1234567800000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedSfixed32List),
      [-1234]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedSfixed64List),
      [-0x1234567800000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedFloatList),
      [1.5]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedDoubleList),
      [-1.5]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedBoolList),
      [true]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedStringList),
      ['hello world']);
359 360
  assertEquals(
      true,
361
      bytesCompare(
362
          msg.getExtension(proto.jspb.test.extendRepeatedBytesList)[0], BYTES));
363 364 365 366 367 368 369 370
  assertEquals(1000,
      msg.getExtension(
          proto.jspb.test.ExtendsWithMessage.repeatedExtensionList)[0]
      .getFoo());
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedForeignEnumList),
      [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);

371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedInt32List),
      [-42]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedInt64List),
      [-0x7fffffff00000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedUint32List),
      [0x80000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedUint64List),
      [0xf000000000000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedSint32List),
      [-100]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedSint64List),
      [-0x8000000000000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedFixed32List),
      [1234]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedFixed64List),
      [0x1234567800000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedSfixed32List),
      [-1234]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedSfixed64List),
      [-0x1234567800000000]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedFloatList),
      [1.5]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedDoubleList),
      [-1.5]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedBoolList),
      [true]);
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendPackedRepeatedForeignEnumList),
      [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
414

415 416 417 418 419 420 421 422 423 424 425 426 427
}


describe('protoBinaryTest', function() {
  /**
   * Tests a basic serialization-deserializaton round-trip with all supported
   * field types (on the TestAllTypes message type).
   */
  it('testRoundTrip', function() {
    var msg = new proto.jspb.test.TestAllTypes();
    fillAllFields(msg);
    var encoded = msg.serializeBinary();
    var decoded = proto.jspb.test.TestAllTypes.deserializeBinary(encoded);
428
    checkAllFields(msg, decoded);
429 430
  });

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  /**
   * Test that base64 string and Uint8Array are interchangeable in bytes fields.
   */
  it('testBytesFieldsGettersInterop', function() {
    var msg = new proto.jspb.test.TestAllTypes();
    // Set from a base64 string and check all the getters work.
    msg.setOptionalBytes(BYTES_B64);
    assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
    assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));

    // Test binary serialize round trip doesn't break it.
    msg = proto.jspb.test.TestAllTypes.deserializeBinary(msg.serializeBinary());
    assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
    assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));

    msg = new proto.jspb.test.TestAllTypes();
    // Set from a Uint8Array and check all the getters work.
    msg.setOptionalBytes(BYTES);
    assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
    assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));

  });

  /**
   * Test that bytes setters will receive result of any of the getters.
   */
  it('testBytesFieldsSettersInterop', function() {
    var msg = new proto.jspb.test.TestAllTypes();
    msg.setOptionalBytes(BYTES);
    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));

    msg.setOptionalBytes(msg.getOptionalBytes());
    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
    msg.setOptionalBytes(msg.getOptionalBytes_asB64());
    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
    msg.setOptionalBytes(msg.getOptionalBytes_asU8());
    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
  });

  /**
   * Test that bytes setters will receive result of any of the getters.
   */
  it('testRepeatedBytesGetters', function() {
    var msg = new proto.jspb.test.TestAllTypes();

    function assertGetters() {
      assertTrue(goog.isString(msg.getRepeatedBytesList_asB64()[0]));
      assertTrue(goog.isString(msg.getRepeatedBytesList_asB64()[1]));
      assertTrue(msg.getRepeatedBytesList_asU8()[0] instanceof Uint8Array);
      assertTrue(msg.getRepeatedBytesList_asU8()[1] instanceof Uint8Array);

      assertTrue(bytesCompare(msg.getRepeatedBytesList()[0], BYTES));
      assertTrue(bytesCompare(msg.getRepeatedBytesList()[1], BYTES));
      assertTrue(bytesCompare(msg.getRepeatedBytesList_asB64()[0], BYTES));
      assertTrue(bytesCompare(msg.getRepeatedBytesList_asB64()[1], BYTES));
      assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[0], BYTES));
      assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[1], BYTES));
    }

    msg.setRepeatedBytesList([BYTES, BYTES]);
    assertGetters();

    msg.setRepeatedBytesList([BYTES_B64, BYTES_B64]);
    assertGetters();

    msg.setRepeatedBytesList(null);
    assertEquals(0, msg.getRepeatedBytesList().length);
    assertEquals(0, msg.getRepeatedBytesList_asB64().length);
    assertEquals(0, msg.getRepeatedBytesList_asU8().length);
  });
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

  /**
   * Helper: fill all extension values.
   * @param {proto.jspb.test.TestExtendable} msg
   */
  function fillExtensions(msg) {
    msg.setExtension(
        proto.jspb.test.extendOptionalInt32, -42);
    msg.setExtension(
        proto.jspb.test.extendOptionalInt64, -0x7fffffff00000000);
    msg.setExtension(
        proto.jspb.test.extendOptionalUint32, 0x80000000);
    msg.setExtension(
        proto.jspb.test.extendOptionalUint64, 0xf000000000000000);
    msg.setExtension(
        proto.jspb.test.extendOptionalSint32, -100);
    msg.setExtension(
        proto.jspb.test.extendOptionalSint64, -0x8000000000000000);
    msg.setExtension(
        proto.jspb.test.extendOptionalFixed32, 1234);
    msg.setExtension(
        proto.jspb.test.extendOptionalFixed64, 0x1234567800000000);
    msg.setExtension(
        proto.jspb.test.extendOptionalSfixed32, -1234);
    msg.setExtension(
        proto.jspb.test.extendOptionalSfixed64, -0x1234567800000000);
    msg.setExtension(
        proto.jspb.test.extendOptionalFloat, 1.5);
    msg.setExtension(
        proto.jspb.test.extendOptionalDouble, -1.5);
    msg.setExtension(
        proto.jspb.test.extendOptionalBool, true);
    msg.setExtension(
        proto.jspb.test.extendOptionalString, 'hello world');
538
    msg.setExtension(proto.jspb.test.extendOptionalBytes, BYTES);
539 540 541 542 543 544 545 546
    var submsg = new proto.jspb.test.ExtendsWithMessage();
    submsg.setFoo(16);
    msg.setExtension(
        proto.jspb.test.ExtendsWithMessage.optionalExtension, submsg);
    msg.setExtension(
        proto.jspb.test.extendOptionalForeignEnum,
        proto.jspb.test.ForeignEnum.FOREIGN_FOO);

547

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    msg.setExtension(
        proto.jspb.test.extendRepeatedInt32List, [-42]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedInt64List, [-0x7fffffff00000000]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedUint32List, [0x80000000]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedUint64List, [0xf000000000000000]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedSint32List, [-100]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedSint64List, [-0x8000000000000000]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedFixed32List, [1234]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedFixed64List, [0x1234567800000000]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedSfixed32List, [-1234]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedSfixed64List, [-0x1234567800000000]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedFloatList, [1.5]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedDoubleList, [-1.5]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedBoolList, [true]);
    msg.setExtension(
        proto.jspb.test.extendRepeatedStringList, ['hello world']);
576
    msg.setExtension(proto.jspb.test.extendRepeatedBytesList, [BYTES]);
577 578 579 580 581 582 583
    submsg = new proto.jspb.test.ExtendsWithMessage();
    submsg.setFoo(1000);
    msg.setExtension(
        proto.jspb.test.ExtendsWithMessage.repeatedExtensionList, [submsg]);
    msg.setExtension(proto.jspb.test.extendRepeatedForeignEnumList,
        [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);

584

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedInt32List, [-42]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedInt64List, [-0x7fffffff00000000]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedUint32List, [0x80000000]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedUint64List, [0xf000000000000000]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedSint32List, [-100]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedSint64List, [-0x8000000000000000]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedFixed32List, [1234]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedFixed64List, [0x1234567800000000]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedSfixed32List, [-1234]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedSfixed64List,
        [-0x1234567800000000]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedFloatList, [1.5]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedDoubleList, [-1.5]);
    msg.setExtension(
        proto.jspb.test.extendPackedRepeatedBoolList, [true]);
    msg.setExtension(proto.jspb.test.extendPackedRepeatedForeignEnumList,
        [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);
614

615 616 617 618 619 620 621 622 623 624 625 626 627 628
  }


  /**
   * Tests extension serialization and deserialization.
   */
  it('testExtensions', function() {
    var msg = new proto.jspb.test.TestExtendable();
    fillExtensions(msg);
    var encoded = msg.serializeBinary();
    var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded);
    checkExtensions(decoded);
  });
});