• 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
31goog.require('goog.crypt.base64');
32goog.require('goog.testing.asserts');
33
34// CommonJS-LoadFromFile: testbinary_pb proto.jspb.test
35goog.require('proto.jspb.test.ForeignMessage');
36
37// CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test
38goog.require('proto.jspb.test.Proto3Enum');
39goog.require('proto.jspb.test.TestProto3');
40
41
42var BYTES = new Uint8Array([1, 2, 8, 9]);
43var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES);
44
45
46/**
47 * Helper: compare a bytes field to an expected value
48 * @param {Uint8Array|string} arr
49 * @param {Uint8Array} expected
50 * @return {boolean}
51 */
52function bytesCompare(arr, expected) {
53  if (typeof arr === 'string') {
54    arr = goog.crypt.base64.decodeStringToUint8Array(arr);
55  }
56  if (arr.length != expected.length) {
57    return false;
58  }
59  for (var i = 0; i < arr.length; i++) {
60    if (arr[i] != expected[i]) {
61      return false;
62    }
63  }
64  return true;
65}
66
67
68describe('proto3Test', function() {
69  /**
70   * Test defaults for proto3 message fields.
71   */
72  it('testProto3FieldDefaults', function() {
73    var msg = new proto.jspb.test.TestProto3();
74
75    assertEquals(msg.getOptionalInt32(), 0);
76    assertEquals(msg.getOptionalInt64(), 0);
77    assertEquals(msg.getOptionalUint32(), 0);
78    assertEquals(msg.getOptionalUint64(), 0);
79    assertEquals(msg.getOptionalSint32(), 0);
80    assertEquals(msg.getOptionalSint64(), 0);
81    assertEquals(msg.getOptionalFixed32(), 0);
82    assertEquals(msg.getOptionalFixed64(), 0);
83    assertEquals(msg.getOptionalSfixed32(), 0);
84    assertEquals(msg.getOptionalSfixed64(), 0);
85    assertEquals(msg.getOptionalFloat(), 0);
86    assertEquals(msg.getOptionalDouble(), 0);
87    assertEquals(msg.getOptionalString(), '');
88
89    // TODO(b/26173701): when we change bytes fields default getter to return
90    // Uint8Array, we'll want to switch this assertion to match the u8 case.
91    assertEquals(typeof msg.getOptionalBytes(), 'string');
92    assertEquals(msg.getOptionalBytes_asU8() instanceof Uint8Array, true);
93    assertEquals(typeof msg.getOptionalBytes_asB64(), 'string');
94    assertEquals(msg.getOptionalBytes().length, 0);
95    assertEquals(msg.getOptionalBytes_asU8().length, 0);
96    assertEquals(msg.getOptionalBytes_asB64(), '');
97
98    assertEquals(msg.getOptionalForeignEnum(),
99                 proto.jspb.test.Proto3Enum.PROTO3_FOO);
100    assertEquals(msg.getOptionalForeignMessage(), undefined);
101    assertEquals(msg.getOptionalForeignMessage(), undefined);
102
103    assertEquals(msg.getRepeatedInt32List().length, 0);
104    assertEquals(msg.getRepeatedInt64List().length, 0);
105    assertEquals(msg.getRepeatedUint32List().length, 0);
106    assertEquals(msg.getRepeatedUint64List().length, 0);
107    assertEquals(msg.getRepeatedSint32List().length, 0);
108    assertEquals(msg.getRepeatedSint64List().length, 0);
109    assertEquals(msg.getRepeatedFixed32List().length, 0);
110    assertEquals(msg.getRepeatedFixed64List().length, 0);
111    assertEquals(msg.getRepeatedSfixed32List().length, 0);
112    assertEquals(msg.getRepeatedSfixed64List().length, 0);
113    assertEquals(msg.getRepeatedFloatList().length, 0);
114    assertEquals(msg.getRepeatedDoubleList().length, 0);
115    assertEquals(msg.getRepeatedStringList().length, 0);
116    assertEquals(msg.getRepeatedBytesList().length, 0);
117    assertEquals(msg.getRepeatedForeignEnumList().length, 0);
118    assertEquals(msg.getRepeatedForeignMessageList().length, 0);
119
120  });
121
122
123  /**
124   * Test that all fields can be set and read via a serialization roundtrip.
125   */
126  it('testProto3FieldSetGet', function() {
127    var msg = new proto.jspb.test.TestProto3();
128
129    msg.setOptionalInt32(-42);
130    msg.setOptionalInt64(-0x7fffffff00000000);
131    msg.setOptionalUint32(0x80000000);
132    msg.setOptionalUint64(0xf000000000000000);
133    msg.setOptionalSint32(-100);
134    msg.setOptionalSint64(-0x8000000000000000);
135    msg.setOptionalFixed32(1234);
136    msg.setOptionalFixed64(0x1234567800000000);
137    msg.setOptionalSfixed32(-1234);
138    msg.setOptionalSfixed64(-0x1234567800000000);
139    msg.setOptionalFloat(1.5);
140    msg.setOptionalDouble(-1.5);
141    msg.setOptionalBool(true);
142    msg.setOptionalString('hello world');
143    msg.setOptionalBytes(BYTES);
144    var submsg = new proto.jspb.test.ForeignMessage();
145    submsg.setC(16);
146    msg.setOptionalForeignMessage(submsg);
147    msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
148
149    msg.setRepeatedInt32List([-42]);
150    msg.setRepeatedInt64List([-0x7fffffff00000000]);
151    msg.setRepeatedUint32List([0x80000000]);
152    msg.setRepeatedUint64List([0xf000000000000000]);
153    msg.setRepeatedSint32List([-100]);
154    msg.setRepeatedSint64List([-0x8000000000000000]);
155    msg.setRepeatedFixed32List([1234]);
156    msg.setRepeatedFixed64List([0x1234567800000000]);
157    msg.setRepeatedSfixed32List([-1234]);
158    msg.setRepeatedSfixed64List([-0x1234567800000000]);
159    msg.setRepeatedFloatList([1.5]);
160    msg.setRepeatedDoubleList([-1.5]);
161    msg.setRepeatedBoolList([true]);
162    msg.setRepeatedStringList(['hello world']);
163    msg.setRepeatedBytesList([BYTES]);
164    submsg = new proto.jspb.test.ForeignMessage();
165    submsg.setC(1000);
166    msg.setRepeatedForeignMessageList([submsg]);
167    msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]);
168
169    msg.setOneofString('asdf');
170
171    var serialized = msg.serializeBinary();
172    msg = proto.jspb.test.TestProto3.deserializeBinary(serialized);
173
174    assertEquals(msg.getOptionalInt32(), -42);
175    assertEquals(msg.getOptionalInt64(), -0x7fffffff00000000);
176    assertEquals(msg.getOptionalUint32(), 0x80000000);
177    assertEquals(msg.getOptionalUint64(), 0xf000000000000000);
178    assertEquals(msg.getOptionalSint32(), -100);
179    assertEquals(msg.getOptionalSint64(), -0x8000000000000000);
180    assertEquals(msg.getOptionalFixed32(), 1234);
181    assertEquals(msg.getOptionalFixed64(), 0x1234567800000000);
182    assertEquals(msg.getOptionalSfixed32(), -1234);
183    assertEquals(msg.getOptionalSfixed64(), -0x1234567800000000);
184    assertEquals(msg.getOptionalFloat(), 1.5);
185    assertEquals(msg.getOptionalDouble(), -1.5);
186    assertEquals(msg.getOptionalBool(), true);
187    assertEquals(msg.getOptionalString(), 'hello world');
188    assertEquals(true, bytesCompare(msg.getOptionalBytes(), BYTES));
189    assertEquals(msg.getOptionalForeignMessage().getC(), 16);
190    assertEquals(msg.getOptionalForeignEnum(),
191        proto.jspb.test.Proto3Enum.PROTO3_BAR);
192
193    assertElementsEquals(msg.getRepeatedInt32List(), [-42]);
194    assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]);
195    assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]);
196    assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]);
197    assertElementsEquals(msg.getRepeatedSint32List(), [-100]);
198    assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]);
199    assertElementsEquals(msg.getRepeatedFixed32List(), [1234]);
200    assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]);
201    assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]);
202    assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]);
203    assertElementsEquals(msg.getRepeatedFloatList(), [1.5]);
204    assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]);
205    assertElementsEquals(msg.getRepeatedBoolList(), [true]);
206    assertElementsEquals(msg.getRepeatedStringList(), ['hello world']);
207    assertEquals(msg.getRepeatedBytesList().length, 1);
208    assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], BYTES));
209    assertEquals(msg.getRepeatedForeignMessageList().length, 1);
210    assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000);
211    assertElementsEquals(msg.getRepeatedForeignEnumList(),
212        [proto.jspb.test.Proto3Enum.PROTO3_BAR]);
213
214    assertEquals(msg.getOneofString(), 'asdf');
215  });
216
217
218  /**
219   * Test that oneofs continue to have a notion of field presence.
220   */
221  it('testOneofs', function() {
222    var msg = new proto.jspb.test.TestProto3();
223
224    assertEquals(msg.getOneofUint32(), 0);
225    assertEquals(msg.getOneofForeignMessage(), undefined);
226    assertEquals(msg.getOneofString(), '');
227    assertEquals(msg.getOneofBytes(), '');
228    assertFalse(msg.hasOneofUint32());
229    assertFalse(msg.hasOneofString());
230    assertFalse(msg.hasOneofBytes());
231
232    msg.setOneofUint32(42);
233    assertEquals(msg.getOneofUint32(), 42);
234    assertEquals(msg.getOneofForeignMessage(), undefined);
235    assertEquals(msg.getOneofString(), '');
236    assertEquals(msg.getOneofBytes(), '');
237    assertTrue(msg.hasOneofUint32());
238    assertFalse(msg.hasOneofString());
239    assertFalse(msg.hasOneofBytes());
240
241
242    var submsg = new proto.jspb.test.ForeignMessage();
243    msg.setOneofForeignMessage(submsg);
244    assertEquals(msg.getOneofUint32(), 0);
245    assertEquals(msg.getOneofForeignMessage(), submsg);
246    assertEquals(msg.getOneofString(), '');
247    assertEquals(msg.getOneofBytes(), '');
248    assertFalse(msg.hasOneofUint32());
249    assertFalse(msg.hasOneofString());
250    assertFalse(msg.hasOneofBytes());
251
252    msg.setOneofString('hello');
253    assertEquals(msg.getOneofUint32(), 0);
254    assertEquals(msg.getOneofForeignMessage(), undefined);
255    assertEquals(msg.getOneofString(), 'hello');
256    assertEquals(msg.getOneofBytes(), '');
257    assertFalse(msg.hasOneofUint32());
258    assertTrue(msg.hasOneofString());
259    assertFalse(msg.hasOneofBytes());
260
261    msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
262    assertEquals(msg.getOneofUint32(), 0);
263    assertEquals(msg.getOneofForeignMessage(), undefined);
264    assertEquals(msg.getOneofString(), '');
265    assertEquals(msg.getOneofBytes_asB64(),
266        goog.crypt.base64.encodeString('\u00FF\u00FF'));
267    assertFalse(msg.hasOneofUint32());
268    assertFalse(msg.hasOneofString());
269    assertTrue(msg.hasOneofBytes());
270  });
271
272
273  /**
274   * Test that "default"-valued primitive fields are not emitted on the wire.
275   */
276  it('testNoSerializeDefaults', function() {
277    var msg = new proto.jspb.test.TestProto3();
278
279    // Set each primitive to a non-default value, then back to its default, to
280    // ensure that the serialization is actually checking the value and not just
281    // whether it has ever been set.
282    msg.setOptionalInt32(42);
283    msg.setOptionalInt32(0);
284    msg.setOptionalDouble(3.14);
285    msg.setOptionalDouble(0.0);
286    msg.setOptionalBool(true);
287    msg.setOptionalBool(false);
288    msg.setOptionalString('hello world');
289    msg.setOptionalString('');
290    msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
291    msg.setOptionalBytes('');
292    msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage());
293    msg.setOptionalForeignMessage(null);
294    msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
295    msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO);
296    msg.setOneofUint32(32);
297    msg.setOneofUint32(null);
298
299
300    var serialized = msg.serializeBinary();
301    assertEquals(0, serialized.length);
302  });
303
304  /**
305   * Test that base64 string and Uint8Array are interchangeable in bytes fields.
306   */
307  it('testBytesFieldsInterop', function() {
308    var msg = new proto.jspb.test.TestProto3();
309    // Set as a base64 string and check all the getters work.
310    msg.setOptionalBytes(BYTES_B64);
311    assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
312    assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
313    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
314
315    // Test binary serialize round trip doesn't break it.
316    msg = proto.jspb.test.TestProto3.deserializeBinary(msg.serializeBinary());
317    assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
318    assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
319    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
320
321    msg = new proto.jspb.test.TestProto3();
322    // Set as a Uint8Array and check all the getters work.
323    msg.setOptionalBytes(BYTES);
324    assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
325    assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
326    assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
327
328  });
329});
330