proto_test.js 26.8 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.BinaryWriter');
36
goog.require('jspb.Message');
37

38
// CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test
39 40 41 42 43
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');
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 90
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');

91
// CommonJS-LoadFromFile: ../node_modules/google-protobuf/google/protobuf/any_pb proto.google.protobuf
92 93
goog.require('proto.google.protobuf.Any');

94 95 96

var suite = {};

97 98 99 100 101
var BYTES = new Uint8Array([1, 2, 8, 9]);

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


102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/**
 * 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');
123
  msg.setOptionalBytes(BYTES);
124 125 126 127 128 129 130 131
  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');

132

133 134 135 136 137 138 139 140 141 142 143 144 145 146
  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']);
147
  msg.setRepeatedBytesList([BYTES, BYTES]);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  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]);
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 177
 * @return {boolean}
 */
178 179 180 181 182
function bytesCompare(arr, expected) {
  if (goog.isString(arr)) {
    arr = goog.crypt.base64.decodeStringToUint8Array(arr);
  }
  if (arr.length != expected.length) {
183 184
    return false;
  }
185 186 187
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] != expected[i]) {
      return false;
188 189
    }
  }
190
  return true;
191 192 193 194 195 196
}


/**
 * Helper: verify contents of given TestAllTypes message as set by
 * fillAllFields().
197 198
 * @param {proto.jspb.test.TestAllTypes} original
 * @param {proto.jspb.test.TestAllTypes} copy
199
 */
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
function checkAllFields(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(),
223
      proto.jspb.test.ForeignEnum.FOREIGN_FOO);
224 225 226 227


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

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  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(),
255 256
      [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);

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

275 276 277

  // Check last so we get more granular errors first.
  assertTrue(jspb.Message.equals(original, copy));
278 279 280 281 282 283 284 285
}


/**
 * Helper: verify that all expected extensions are present.
 * @param {!proto.jspb.test.TestExtendable} msg
 */
function checkExtensions(msg) {
286
  assertEquals(0, msg.getExtension(proto.jspb.test.extendOptionalInt32));
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
  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));
313 314 315
  assertEquals(
      true, bytesCompare(
                msg.getExtension(proto.jspb.test.extendOptionalBytes), BYTES));
316 317 318
  assertEquals(16,
      msg.getExtension(
          proto.jspb.test.ExtendsWithMessage.optionalExtension).getFoo());
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 359 360 361 362

  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']);
363 364
  assertEquals(
      true,
365
      bytesCompare(
366
          msg.getExtension(proto.jspb.test.extendRepeatedBytesList)[0], BYTES));
367 368 369 370 371 372 373 374
  assertEquals(1000,
      msg.getExtension(
          proto.jspb.test.ExtendsWithMessage.repeatedExtensionList)[0]
      .getFoo());
  assertElementsEquals(
      msg.getExtension(proto.jspb.test.extendRepeatedForeignEnumList),
      [proto.jspb.test.ForeignEnum.FOREIGN_FOO]);

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 414 415 416 417
  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]);
418

419 420 421 422 423 424 425 426 427 428 429 430 431
}


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);
432
    checkAllFields(msg, decoded);
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
  /**
   * 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();

Bo Yang's avatar
Bo Yang committed
503
    msg.setRepeatedBytesList([]);
504 505 506 507
    assertEquals(0, msg.getRepeatedBytesList().length);
    assertEquals(0, msg.getRepeatedBytesList_asB64().length);
    assertEquals(0, msg.getRepeatedBytesList_asU8().length);
  });
508 509 510 511 512 513

  /**
   * Helper: fill all extension values.
   * @param {proto.jspb.test.TestExtendable} msg
   */
  function fillExtensions(msg) {
514
    msg.setExtension(proto.jspb.test.extendOptionalInt32, 0);
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    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');
541
    msg.setExtension(proto.jspb.test.extendOptionalBytes, BYTES);
542 543 544 545 546 547 548 549
    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);

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 576 577 578
    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']);
579
    msg.setExtension(proto.jspb.test.extendRepeatedBytesList, [BYTES]);
580 581 582 583 584 585 586
    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]);

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 614 615 616
    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]);
617

618 619 620 621 622 623 624 625 626 627 628 629 630
  }


  /**
   * 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);
  });
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

  /**
   * Tests that unknown extensions don't cause deserialization failure.
   */
  it('testUnknownExtension', function() {
    var msg = new proto.jspb.test.TestExtendable();
    fillExtensions(msg);
    var writer = new jspb.BinaryWriter();
    writer.writeBool((1 << 29) - 1, true);
    proto.jspb.test.TestExtendable.serializeBinaryToWriter(msg, writer);
    var encoded = writer.getResultBuffer();
    var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded);
    checkExtensions(decoded);
  });

  it('testAnyWellKnownType', function() {
    var any = new proto.google.protobuf.Any();
    var msg = new proto.jspb.test.TestAllTypes();

    fillAllFields(msg);

    any.pack(msg.serializeBinary(), 'jspb.test.TestAllTypes');

    assertEquals('type.googleapis.com/jspb.test.TestAllTypes',
                 any.getTypeUrl());

    var msg2 = any.unpack(
        proto.jspb.test.TestAllTypes.deserializeBinary,
        'jspb.test.TestAllTypes');

    checkAllFields(msg, msg2);
  });
663
});