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 31goog.require('goog.crypt.base64'); 32goog.require('goog.testing.asserts'); 33// CommonJS-LoadFromFile: testbinary_pb proto.jspb.test 34goog.require('proto.jspb.test.ForeignMessage'); 35// CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test 36goog.require('proto.jspb.test.Proto3Enum'); 37goog.require('proto.jspb.test.TestProto3'); 38// CommonJS-LoadFromFile: google/protobuf/any_pb proto.google.protobuf 39goog.require('proto.google.protobuf.Any'); 40// CommonJS-LoadFromFile: google/protobuf/timestamp_pb proto.google.protobuf 41goog.require('proto.google.protobuf.Timestamp'); 42// CommonJS-LoadFromFile: google/protobuf/struct_pb proto.google.protobuf 43goog.require('proto.google.protobuf.Struct'); 44 45 46var BYTES = new Uint8Array([1, 2, 8, 9]); 47var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); 48 49 50/** 51 * Helper: compare a bytes field to an expected value 52 * @param {Uint8Array|string} arr 53 * @param {Uint8Array} expected 54 * @return {boolean} 55 */ 56function bytesCompare(arr, expected) { 57 if (goog.isString(arr)) { 58 arr = goog.crypt.base64.decodeStringToUint8Array(arr); 59 } 60 if (arr.length != expected.length) { 61 return false; 62 } 63 for (var i = 0; i < arr.length; i++) { 64 if (arr[i] != expected[i]) { 65 return false; 66 } 67 } 68 return true; 69} 70 71 72describe('proto3Test', function() { 73 74 /** 75 * Test default values don't affect equality test. 76 */ 77 it('testEqualsProto3', function() { 78 var msg1 = new proto.jspb.test.TestProto3(); 79 var msg2 = new proto.jspb.test.TestProto3(); 80 msg2.setOptionalString(''); 81 82 assertTrue(jspb.Message.equals(msg1, msg2)); 83 }); 84 85 86 /** 87 * Test setting when a field has default semantics. 88 */ 89 it('testSetProto3ToValueAndBackToDefault', function() { 90 var msg = new proto.jspb.test.TestProto3(); 91 92 // Setting should work normally. 93 msg.setOptionalString('optionalString'); 94 assertEquals(msg.getOptionalString(), 'optionalString'); 95 96 // Clearing should work too ... 97 msg.setOptionalString(''); 98 assertEquals(msg.getOptionalString(), ''); 99 100 // ... and shouldn't affect the equality with a brand new message. 101 assertTrue(jspb.Message.equals(msg, new proto.jspb.test.TestProto3())); 102 }); 103 104 /** 105 * Test defaults for proto3 message fields. 106 */ 107 it('testProto3FieldDefaults', function() { 108 var msg = new proto.jspb.test.TestProto3(); 109 110 assertEquals(msg.getOptionalInt32(), 0); 111 assertEquals(msg.getOptionalInt64(), 0); 112 assertEquals(msg.getOptionalUint32(), 0); 113 assertEquals(msg.getOptionalUint64(), 0); 114 assertEquals(msg.getOptionalSint32(), 0); 115 assertEquals(msg.getOptionalSint64(), 0); 116 assertEquals(msg.getOptionalFixed32(), 0); 117 assertEquals(msg.getOptionalFixed64(), 0); 118 assertEquals(msg.getOptionalSfixed32(), 0); 119 assertEquals(msg.getOptionalSfixed64(), 0); 120 assertEquals(msg.getOptionalFloat(), 0); 121 assertEquals(msg.getOptionalDouble(), 0); 122 assertEquals(msg.getOptionalString(), ''); 123 124 // TODO(b/26173701): when we change bytes fields default getter to return 125 // Uint8Array, we'll want to switch this assertion to match the u8 case. 126 assertEquals(typeof msg.getOptionalBytes(), 'string'); 127 assertEquals(msg.getOptionalBytes_asU8() instanceof Uint8Array, true); 128 assertEquals(typeof msg.getOptionalBytes_asB64(), 'string'); 129 assertEquals(msg.getOptionalBytes().length, 0); 130 assertEquals(msg.getOptionalBytes_asU8().length, 0); 131 assertEquals(msg.getOptionalBytes_asB64(), ''); 132 133 assertEquals(msg.getOptionalForeignEnum(), 134 proto.jspb.test.Proto3Enum.PROTO3_FOO); 135 assertEquals(msg.getOptionalForeignMessage(), undefined); 136 assertEquals(msg.getOptionalForeignMessage(), undefined); 137 138 assertEquals(msg.getRepeatedInt32List().length, 0); 139 assertEquals(msg.getRepeatedInt64List().length, 0); 140 assertEquals(msg.getRepeatedUint32List().length, 0); 141 assertEquals(msg.getRepeatedUint64List().length, 0); 142 assertEquals(msg.getRepeatedSint32List().length, 0); 143 assertEquals(msg.getRepeatedSint64List().length, 0); 144 assertEquals(msg.getRepeatedFixed32List().length, 0); 145 assertEquals(msg.getRepeatedFixed64List().length, 0); 146 assertEquals(msg.getRepeatedSfixed32List().length, 0); 147 assertEquals(msg.getRepeatedSfixed64List().length, 0); 148 assertEquals(msg.getRepeatedFloatList().length, 0); 149 assertEquals(msg.getRepeatedDoubleList().length, 0); 150 assertEquals(msg.getRepeatedStringList().length, 0); 151 assertEquals(msg.getRepeatedBytesList().length, 0); 152 assertEquals(msg.getRepeatedForeignEnumList().length, 0); 153 assertEquals(msg.getRepeatedForeignMessageList().length, 0); 154 155 }); 156 157 158 /** 159 * Test that all fields can be set and read via a serialization roundtrip. 160 */ 161 it('testProto3FieldSetGet', function() { 162 var msg = new proto.jspb.test.TestProto3(); 163 164 msg.setOptionalInt32(-42); 165 msg.setOptionalInt64(-0x7fffffff00000000); 166 msg.setOptionalUint32(0x80000000); 167 msg.setOptionalUint64(0xf000000000000000); 168 msg.setOptionalSint32(-100); 169 msg.setOptionalSint64(-0x8000000000000000); 170 msg.setOptionalFixed32(1234); 171 msg.setOptionalFixed64(0x1234567800000000); 172 msg.setOptionalSfixed32(-1234); 173 msg.setOptionalSfixed64(-0x1234567800000000); 174 msg.setOptionalFloat(1.5); 175 msg.setOptionalDouble(-1.5); 176 msg.setOptionalBool(true); 177 msg.setOptionalString('hello world'); 178 msg.setOptionalBytes(BYTES); 179 var submsg = new proto.jspb.test.ForeignMessage(); 180 submsg.setC(16); 181 msg.setOptionalForeignMessage(submsg); 182 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); 183 184 msg.setRepeatedInt32List([-42]); 185 msg.setRepeatedInt64List([-0x7fffffff00000000]); 186 msg.setRepeatedUint32List([0x80000000]); 187 msg.setRepeatedUint64List([0xf000000000000000]); 188 msg.setRepeatedSint32List([-100]); 189 msg.setRepeatedSint64List([-0x8000000000000000]); 190 msg.setRepeatedFixed32List([1234]); 191 msg.setRepeatedFixed64List([0x1234567800000000]); 192 msg.setRepeatedSfixed32List([-1234]); 193 msg.setRepeatedSfixed64List([-0x1234567800000000]); 194 msg.setRepeatedFloatList([1.5]); 195 msg.setRepeatedDoubleList([-1.5]); 196 msg.setRepeatedBoolList([true]); 197 msg.setRepeatedStringList(['hello world']); 198 msg.setRepeatedBytesList([BYTES]); 199 submsg = new proto.jspb.test.ForeignMessage(); 200 submsg.setC(1000); 201 msg.setRepeatedForeignMessageList([submsg]); 202 msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]); 203 204 msg.setOneofString('asdf'); 205 206 var serialized = msg.serializeBinary(); 207 msg = proto.jspb.test.TestProto3.deserializeBinary(serialized); 208 209 assertEquals(msg.getOptionalInt32(), -42); 210 assertEquals(msg.getOptionalInt64(), -0x7fffffff00000000); 211 assertEquals(msg.getOptionalUint32(), 0x80000000); 212 assertEquals(msg.getOptionalUint64(), 0xf000000000000000); 213 assertEquals(msg.getOptionalSint32(), -100); 214 assertEquals(msg.getOptionalSint64(), -0x8000000000000000); 215 assertEquals(msg.getOptionalFixed32(), 1234); 216 assertEquals(msg.getOptionalFixed64(), 0x1234567800000000); 217 assertEquals(msg.getOptionalSfixed32(), -1234); 218 assertEquals(msg.getOptionalSfixed64(), -0x1234567800000000); 219 assertEquals(msg.getOptionalFloat(), 1.5); 220 assertEquals(msg.getOptionalDouble(), -1.5); 221 assertEquals(msg.getOptionalBool(), true); 222 assertEquals(msg.getOptionalString(), 'hello world'); 223 assertEquals(true, bytesCompare(msg.getOptionalBytes(), BYTES)); 224 assertEquals(msg.getOptionalForeignMessage().getC(), 16); 225 assertEquals(msg.getOptionalForeignEnum(), 226 proto.jspb.test.Proto3Enum.PROTO3_BAR); 227 228 assertElementsEquals(msg.getRepeatedInt32List(), [-42]); 229 assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]); 230 assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]); 231 assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]); 232 assertElementsEquals(msg.getRepeatedSint32List(), [-100]); 233 assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]); 234 assertElementsEquals(msg.getRepeatedFixed32List(), [1234]); 235 assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]); 236 assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]); 237 assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]); 238 assertElementsEquals(msg.getRepeatedFloatList(), [1.5]); 239 assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]); 240 assertElementsEquals(msg.getRepeatedBoolList(), [true]); 241 assertElementsEquals(msg.getRepeatedStringList(), ['hello world']); 242 assertEquals(msg.getRepeatedBytesList().length, 1); 243 assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], BYTES)); 244 assertEquals(msg.getRepeatedForeignMessageList().length, 1); 245 assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000); 246 assertElementsEquals(msg.getRepeatedForeignEnumList(), 247 [proto.jspb.test.Proto3Enum.PROTO3_BAR]); 248 249 assertEquals(msg.getOneofString(), 'asdf'); 250 }); 251 252 253 /** 254 * Test that oneofs continue to have a notion of field presence. 255 */ 256 it('testOneofs', function() { 257 // Default instance. 258 var msg = new proto.jspb.test.TestProto3(); 259 assertEquals(msg.getOneofUint32(), 0); 260 assertEquals(msg.getOneofForeignMessage(), undefined); 261 assertEquals(msg.getOneofString(), ''); 262 assertEquals(msg.getOneofBytes(), ''); 263 264 assertFalse(msg.hasOneofUint32()); 265 assertFalse(msg.hasOneofForeignMessage()); 266 assertFalse(msg.hasOneofString()); 267 assertFalse(msg.hasOneofBytes()); 268 269 // Integer field. 270 msg.setOneofUint32(42); 271 assertEquals(msg.getOneofUint32(), 42); 272 assertEquals(msg.getOneofForeignMessage(), undefined); 273 assertEquals(msg.getOneofString(), ''); 274 assertEquals(msg.getOneofBytes(), ''); 275 276 assertTrue(msg.hasOneofUint32()); 277 assertFalse(msg.hasOneofForeignMessage()); 278 assertFalse(msg.hasOneofString()); 279 assertFalse(msg.hasOneofBytes()); 280 281 // Sub-message field. 282 var submsg = new proto.jspb.test.ForeignMessage(); 283 msg.setOneofForeignMessage(submsg); 284 assertEquals(msg.getOneofUint32(), 0); 285 assertEquals(msg.getOneofForeignMessage(), submsg); 286 assertEquals(msg.getOneofString(), ''); 287 assertEquals(msg.getOneofBytes(), ''); 288 289 assertFalse(msg.hasOneofUint32()); 290 assertTrue(msg.hasOneofForeignMessage()); 291 assertFalse(msg.hasOneofString()); 292 assertFalse(msg.hasOneofBytes()); 293 294 // String field. 295 msg.setOneofString('hello'); 296 assertEquals(msg.getOneofUint32(), 0); 297 assertEquals(msg.getOneofForeignMessage(), undefined); 298 assertEquals(msg.getOneofString(), 'hello'); 299 assertEquals(msg.getOneofBytes(), ''); 300 301 assertFalse(msg.hasOneofUint32()); 302 assertFalse(msg.hasOneofForeignMessage()); 303 assertTrue(msg.hasOneofString()); 304 assertFalse(msg.hasOneofBytes()); 305 306 // Bytes field. 307 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); 308 assertEquals(msg.getOneofUint32(), 0); 309 assertEquals(msg.getOneofForeignMessage(), undefined); 310 assertEquals(msg.getOneofString(), ''); 311 assertEquals(msg.getOneofBytes_asB64(), 312 goog.crypt.base64.encodeString('\u00FF\u00FF')); 313 314 assertFalse(msg.hasOneofUint32()); 315 assertFalse(msg.hasOneofForeignMessage()); 316 assertFalse(msg.hasOneofString()); 317 assertTrue(msg.hasOneofBytes()); 318 }); 319 320 321 /** 322 * Test that "default"-valued primitive fields are not emitted on the wire. 323 */ 324 it('testNoSerializeDefaults', function() { 325 var msg = new proto.jspb.test.TestProto3(); 326 327 // Set each primitive to a non-default value, then back to its default, to 328 // ensure that the serialization is actually checking the value and not just 329 // whether it has ever been set. 330 msg.setOptionalInt32(42); 331 msg.setOptionalInt32(0); 332 msg.setOptionalDouble(3.14); 333 msg.setOptionalDouble(0.0); 334 msg.setOptionalBool(true); 335 msg.setOptionalBool(false); 336 msg.setOptionalString('hello world'); 337 msg.setOptionalString(''); 338 msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); 339 msg.setOptionalBytes(''); 340 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); 341 msg.setOptionalForeignMessage(null); 342 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); 343 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); 344 msg.setOneofUint32(32); 345 msg.clearOneofUint32(); 346 347 348 var serialized = msg.serializeBinary(); 349 assertEquals(0, serialized.length); 350 }); 351 352 /** 353 * Test that base64 string and Uint8Array are interchangeable in bytes fields. 354 */ 355 it('testBytesFieldsInterop', function() { 356 var msg = new proto.jspb.test.TestProto3(); 357 // Set as a base64 string and check all the getters work. 358 msg.setOptionalBytes(BYTES_B64); 359 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); 360 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); 361 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); 362 363 // Test binary serialize round trip doesn't break it. 364 msg = proto.jspb.test.TestProto3.deserializeBinary(msg.serializeBinary()); 365 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); 366 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); 367 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); 368 369 msg = new proto.jspb.test.TestProto3(); 370 // Set as a Uint8Array and check all the getters work. 371 msg.setOptionalBytes(BYTES); 372 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); 373 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); 374 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); 375 376 }); 377 378 379 it('testTimestampWellKnownType', function() { 380 var msg = new proto.google.protobuf.Timestamp(); 381 msg.fromDate(new Date(123456789)); 382 assertEquals(123456, msg.getSeconds()); 383 assertEquals(789000000, msg.getNanos()); 384 var date = msg.toDate(); 385 assertEquals(123456789, date.getTime()); 386 var anotherMsg = proto.google.protobuf.Timestamp.fromDate(date); 387 assertEquals(msg.getSeconds(), anotherMsg.getSeconds()); 388 assertEquals(msg.getNanos(), anotherMsg.getNanos()); 389 }); 390 391 it('testStructWellKnownType', function() { 392 var jsObj = { 393 abc: "def", 394 number: 12345.678, 395 nullKey: null, 396 boolKey: true, 397 listKey: [1, null, true, false, "abc"], 398 structKey: {foo: "bar", somenum: 123}, 399 complicatedKey: [{xyz: {abc: [3, 4, null, false]}}, "zzz"] 400 }; 401 402 var struct = proto.google.protobuf.Struct.fromJavaScript(jsObj); 403 var jsObj2 = struct.toJavaScript(); 404 405 assertEquals("def", jsObj2.abc); 406 assertEquals(12345.678, jsObj2.number); 407 assertEquals(null, jsObj2.nullKey); 408 assertEquals(true, jsObj2.boolKey); 409 assertEquals("abc", jsObj2.listKey[4]); 410 assertEquals("bar", jsObj2.structKey.foo); 411 assertEquals(4, jsObj2.complicatedKey[0].xyz.abc[1]); 412 }); 413}); 414