OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 10 matching lines...) Expand all Loading... |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
| 31 goog.require('goog.crypt.base64'); |
31 goog.require('goog.testing.asserts'); | 32 goog.require('goog.testing.asserts'); |
| 33 |
| 34 // CommonJS-LoadFromFile: testbinary_pb proto.jspb.test |
32 goog.require('proto.jspb.test.ForeignMessage'); | 35 goog.require('proto.jspb.test.ForeignMessage'); |
| 36 |
| 37 // CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test |
33 goog.require('proto.jspb.test.Proto3Enum'); | 38 goog.require('proto.jspb.test.Proto3Enum'); |
34 goog.require('proto.jspb.test.TestProto3'); | 39 goog.require('proto.jspb.test.TestProto3'); |
35 | 40 |
| 41 |
| 42 var BYTES = new Uint8Array([1, 2, 8, 9]); |
| 43 var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); |
| 44 |
| 45 |
36 /** | 46 /** |
37 * Helper: compare a bytes field to a string with codepoints 0--255. | 47 * Helper: compare a bytes field to an expected value |
38 * @param {Uint8Array|string} arr | 48 * @param {Uint8Array|string} arr |
39 * @param {string} str | 49 * @param {Uint8Array} expected |
40 * @return {boolean} | 50 * @return {boolean} |
41 */ | 51 */ |
42 function bytesCompare(arr, str) { | 52 function bytesCompare(arr, expected) { |
43 if (arr.length != str.length) { | 53 if (goog.isString(arr)) { |
| 54 arr = goog.crypt.base64.decodeStringToUint8Array(arr); |
| 55 } |
| 56 if (arr.length != expected.length) { |
44 return false; | 57 return false; |
45 } | 58 } |
46 if (typeof arr == 'string') { | 59 for (var i = 0; i < arr.length; i++) { |
47 for (var i = 0; i < arr.length; i++) { | 60 if (arr[i] != expected[i]) { |
48 if (arr.charCodeAt(i) != str.charCodeAt(i)) { | 61 return false; |
49 return false; | |
50 } | |
51 } | 62 } |
52 return true; | |
53 } else { | |
54 for (var i = 0; i < arr.length; i++) { | |
55 if (arr[i] != str.charCodeAt(i)) { | |
56 return false; | |
57 } | |
58 } | |
59 return true; | |
60 } | 63 } |
| 64 return true; |
61 } | 65 } |
62 | 66 |
63 | 67 |
64 describe('proto3Test', function() { | 68 describe('proto3Test', function() { |
65 /** | 69 /** |
66 * Test defaults for proto3 message fields. | 70 * Test defaults for proto3 message fields. |
67 */ | 71 */ |
68 it('testProto3FieldDefaults', function() { | 72 it('testProto3FieldDefaults', function() { |
69 var msg = new proto.jspb.test.TestProto3(); | 73 var msg = new proto.jspb.test.TestProto3(); |
70 | 74 |
71 assertEquals(msg.getOptionalInt32(), 0); | 75 assertEquals(msg.getOptionalInt32(), 0); |
72 assertEquals(msg.getOptionalInt64(), 0); | 76 assertEquals(msg.getOptionalInt64(), 0); |
73 assertEquals(msg.getOptionalUint32(), 0); | 77 assertEquals(msg.getOptionalUint32(), 0); |
74 assertEquals(msg.getOptionalUint64(), 0); | 78 assertEquals(msg.getOptionalUint64(), 0); |
75 assertEquals(msg.getOptionalSint32(), 0); | 79 assertEquals(msg.getOptionalSint32(), 0); |
76 assertEquals(msg.getOptionalSint64(), 0); | 80 assertEquals(msg.getOptionalSint64(), 0); |
77 assertEquals(msg.getOptionalFixed32(), 0); | 81 assertEquals(msg.getOptionalFixed32(), 0); |
78 assertEquals(msg.getOptionalFixed64(), 0); | 82 assertEquals(msg.getOptionalFixed64(), 0); |
79 assertEquals(msg.getOptionalSfixed32(), 0); | 83 assertEquals(msg.getOptionalSfixed32(), 0); |
80 assertEquals(msg.getOptionalSfixed64(), 0); | 84 assertEquals(msg.getOptionalSfixed64(), 0); |
81 assertEquals(msg.getOptionalFloat(), 0); | 85 assertEquals(msg.getOptionalFloat(), 0); |
82 assertEquals(msg.getOptionalDouble(), 0); | 86 assertEquals(msg.getOptionalDouble(), 0); |
83 assertEquals(msg.getOptionalString(), ''); | 87 assertEquals(msg.getOptionalString(), ''); |
84 | 88 |
85 // If/when we change bytes fields to return Uint8Array, we'll want to switch | 89 // TODO(b/26173701): when we change bytes fields default getter to return |
86 // to this assertion instead: | 90 // Uint8Array, we'll want to switch this assertion to match the u8 case. |
87 //assertEquals(msg.getOptionalBytes() instanceof Uint8Array, true); | |
88 assertEquals(typeof msg.getOptionalBytes(), 'string'); | 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(), ''); |
89 | 97 |
90 assertEquals(msg.getOptionalBytes().length, 0); | 98 assertEquals(msg.getOptionalForeignEnum(), |
91 assertEquals(msg.getOptionalForeignEnum(), proto.jspb.test.Proto3Enum.PROTO3
_FOO); | 99 proto.jspb.test.Proto3Enum.PROTO3_FOO); |
92 assertEquals(msg.getOptionalForeignMessage(), undefined); | 100 assertEquals(msg.getOptionalForeignMessage(), undefined); |
93 assertEquals(msg.getOptionalForeignMessage(), undefined); | 101 assertEquals(msg.getOptionalForeignMessage(), undefined); |
94 | 102 |
95 assertEquals(msg.getRepeatedInt32List().length, 0); | 103 assertEquals(msg.getRepeatedInt32List().length, 0); |
96 assertEquals(msg.getRepeatedInt64List().length, 0); | 104 assertEquals(msg.getRepeatedInt64List().length, 0); |
97 assertEquals(msg.getRepeatedUint32List().length, 0); | 105 assertEquals(msg.getRepeatedUint32List().length, 0); |
98 assertEquals(msg.getRepeatedUint64List().length, 0); | 106 assertEquals(msg.getRepeatedUint64List().length, 0); |
99 assertEquals(msg.getRepeatedSint32List().length, 0); | 107 assertEquals(msg.getRepeatedSint32List().length, 0); |
100 assertEquals(msg.getRepeatedSint64List().length, 0); | 108 assertEquals(msg.getRepeatedSint64List().length, 0); |
101 assertEquals(msg.getRepeatedFixed32List().length, 0); | 109 assertEquals(msg.getRepeatedFixed32List().length, 0); |
(...skipping 23 matching lines...) Expand all Loading... |
125 msg.setOptionalSint32(-100); | 133 msg.setOptionalSint32(-100); |
126 msg.setOptionalSint64(-0x8000000000000000); | 134 msg.setOptionalSint64(-0x8000000000000000); |
127 msg.setOptionalFixed32(1234); | 135 msg.setOptionalFixed32(1234); |
128 msg.setOptionalFixed64(0x1234567800000000); | 136 msg.setOptionalFixed64(0x1234567800000000); |
129 msg.setOptionalSfixed32(-1234); | 137 msg.setOptionalSfixed32(-1234); |
130 msg.setOptionalSfixed64(-0x1234567800000000); | 138 msg.setOptionalSfixed64(-0x1234567800000000); |
131 msg.setOptionalFloat(1.5); | 139 msg.setOptionalFloat(1.5); |
132 msg.setOptionalDouble(-1.5); | 140 msg.setOptionalDouble(-1.5); |
133 msg.setOptionalBool(true); | 141 msg.setOptionalBool(true); |
134 msg.setOptionalString('hello world'); | 142 msg.setOptionalString('hello world'); |
135 msg.setOptionalBytes('bytes'); | 143 msg.setOptionalBytes(BYTES); |
136 var submsg = new proto.jspb.test.ForeignMessage(); | 144 var submsg = new proto.jspb.test.ForeignMessage(); |
137 submsg.setC(16); | 145 submsg.setC(16); |
138 msg.setOptionalForeignMessage(submsg); | 146 msg.setOptionalForeignMessage(submsg); |
139 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); | 147 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); |
140 | 148 |
141 msg.setRepeatedInt32List([-42]); | 149 msg.setRepeatedInt32List([-42]); |
142 msg.setRepeatedInt64List([-0x7fffffff00000000]); | 150 msg.setRepeatedInt64List([-0x7fffffff00000000]); |
143 msg.setRepeatedUint32List([0x80000000]); | 151 msg.setRepeatedUint32List([0x80000000]); |
144 msg.setRepeatedUint64List([0xf000000000000000]); | 152 msg.setRepeatedUint64List([0xf000000000000000]); |
145 msg.setRepeatedSint32List([-100]); | 153 msg.setRepeatedSint32List([-100]); |
146 msg.setRepeatedSint64List([-0x8000000000000000]); | 154 msg.setRepeatedSint64List([-0x8000000000000000]); |
147 msg.setRepeatedFixed32List([1234]); | 155 msg.setRepeatedFixed32List([1234]); |
148 msg.setRepeatedFixed64List([0x1234567800000000]); | 156 msg.setRepeatedFixed64List([0x1234567800000000]); |
149 msg.setRepeatedSfixed32List([-1234]); | 157 msg.setRepeatedSfixed32List([-1234]); |
150 msg.setRepeatedSfixed64List([-0x1234567800000000]); | 158 msg.setRepeatedSfixed64List([-0x1234567800000000]); |
151 msg.setRepeatedFloatList([1.5]); | 159 msg.setRepeatedFloatList([1.5]); |
152 msg.setRepeatedDoubleList([-1.5]); | 160 msg.setRepeatedDoubleList([-1.5]); |
153 msg.setRepeatedBoolList([true]); | 161 msg.setRepeatedBoolList([true]); |
154 msg.setRepeatedStringList(['hello world']); | 162 msg.setRepeatedStringList(['hello world']); |
155 msg.setRepeatedBytesList(['bytes']); | 163 msg.setRepeatedBytesList([BYTES]); |
156 submsg = new proto.jspb.test.ForeignMessage(); | 164 submsg = new proto.jspb.test.ForeignMessage(); |
157 submsg.setC(1000); | 165 submsg.setC(1000); |
158 msg.setRepeatedForeignMessageList([submsg]); | 166 msg.setRepeatedForeignMessageList([submsg]); |
159 msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]); | 167 msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]); |
160 | 168 |
161 msg.setOneofString('asdf'); | 169 msg.setOneofString('asdf'); |
162 | 170 |
163 var serialized = msg.serializeBinary(); | 171 var serialized = msg.serializeBinary(); |
164 msg = proto.jspb.test.TestProto3.deserializeBinary(serialized); | 172 msg = proto.jspb.test.TestProto3.deserializeBinary(serialized); |
165 | 173 |
166 assertEquals(msg.getOptionalInt32(), -42); | 174 assertEquals(msg.getOptionalInt32(), -42); |
167 assertEquals(msg.getOptionalInt64(), -0x7fffffff00000000); | 175 assertEquals(msg.getOptionalInt64(), -0x7fffffff00000000); |
168 assertEquals(msg.getOptionalUint32(), 0x80000000); | 176 assertEquals(msg.getOptionalUint32(), 0x80000000); |
169 assertEquals(msg.getOptionalUint64(), 0xf000000000000000); | 177 assertEquals(msg.getOptionalUint64(), 0xf000000000000000); |
170 assertEquals(msg.getOptionalSint32(), -100); | 178 assertEquals(msg.getOptionalSint32(), -100); |
171 assertEquals(msg.getOptionalSint64(), -0x8000000000000000); | 179 assertEquals(msg.getOptionalSint64(), -0x8000000000000000); |
172 assertEquals(msg.getOptionalFixed32(), 1234); | 180 assertEquals(msg.getOptionalFixed32(), 1234); |
173 assertEquals(msg.getOptionalFixed64(), 0x1234567800000000); | 181 assertEquals(msg.getOptionalFixed64(), 0x1234567800000000); |
174 assertEquals(msg.getOptionalSfixed32(), -1234); | 182 assertEquals(msg.getOptionalSfixed32(), -1234); |
175 assertEquals(msg.getOptionalSfixed64(), -0x1234567800000000); | 183 assertEquals(msg.getOptionalSfixed64(), -0x1234567800000000); |
176 assertEquals(msg.getOptionalFloat(), 1.5); | 184 assertEquals(msg.getOptionalFloat(), 1.5); |
177 assertEquals(msg.getOptionalDouble(), -1.5); | 185 assertEquals(msg.getOptionalDouble(), -1.5); |
178 assertEquals(msg.getOptionalBool(), true); | 186 assertEquals(msg.getOptionalBool(), true); |
179 assertEquals(msg.getOptionalString(), 'hello world'); | 187 assertEquals(msg.getOptionalString(), 'hello world'); |
180 assertEquals(true, bytesCompare(msg.getOptionalBytes(), 'bytes')); | 188 assertEquals(true, bytesCompare(msg.getOptionalBytes(), BYTES)); |
181 assertEquals(msg.getOptionalForeignMessage().getC(), 16); | 189 assertEquals(msg.getOptionalForeignMessage().getC(), 16); |
182 assertEquals(msg.getOptionalForeignEnum(), | 190 assertEquals(msg.getOptionalForeignEnum(), |
183 proto.jspb.test.Proto3Enum.PROTO3_BAR); | 191 proto.jspb.test.Proto3Enum.PROTO3_BAR); |
184 | 192 |
185 assertElementsEquals(msg.getRepeatedInt32List(), [-42]); | 193 assertElementsEquals(msg.getRepeatedInt32List(), [-42]); |
186 assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]); | 194 assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]); |
187 assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]); | 195 assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]); |
188 assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]); | 196 assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]); |
189 assertElementsEquals(msg.getRepeatedSint32List(), [-100]); | 197 assertElementsEquals(msg.getRepeatedSint32List(), [-100]); |
190 assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]); | 198 assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]); |
191 assertElementsEquals(msg.getRepeatedFixed32List(), [1234]); | 199 assertElementsEquals(msg.getRepeatedFixed32List(), [1234]); |
192 assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]); | 200 assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]); |
193 assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]); | 201 assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]); |
194 assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]); | 202 assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]); |
195 assertElementsEquals(msg.getRepeatedFloatList(), [1.5]); | 203 assertElementsEquals(msg.getRepeatedFloatList(), [1.5]); |
196 assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]); | 204 assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]); |
197 assertElementsEquals(msg.getRepeatedBoolList(), [true]); | 205 assertElementsEquals(msg.getRepeatedBoolList(), [true]); |
198 assertElementsEquals(msg.getRepeatedStringList(), ['hello world']); | 206 assertElementsEquals(msg.getRepeatedStringList(), ['hello world']); |
199 assertEquals(msg.getRepeatedBytesList().length, 1); | 207 assertEquals(msg.getRepeatedBytesList().length, 1); |
200 assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], 'bytes')); | 208 assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], BYTES)); |
201 assertEquals(msg.getRepeatedForeignMessageList().length, 1); | 209 assertEquals(msg.getRepeatedForeignMessageList().length, 1); |
202 assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000); | 210 assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000); |
203 assertElementsEquals(msg.getRepeatedForeignEnumList(), | 211 assertElementsEquals(msg.getRepeatedForeignEnumList(), |
204 [proto.jspb.test.Proto3Enum.PROTO3_BAR]); | 212 [proto.jspb.test.Proto3Enum.PROTO3_BAR]); |
205 | 213 |
206 assertEquals(msg.getOneofString(), 'asdf'); | 214 assertEquals(msg.getOneofString(), 'asdf'); |
207 }); | 215 }); |
208 | 216 |
209 | 217 |
210 /** | 218 /** |
(...skipping 20 matching lines...) Expand all Loading... |
231 assertEquals(msg.getOneofForeignMessage(), submsg); | 239 assertEquals(msg.getOneofForeignMessage(), submsg); |
232 assertEquals(msg.getOneofString(), undefined); | 240 assertEquals(msg.getOneofString(), undefined); |
233 assertEquals(msg.getOneofBytes(), undefined); | 241 assertEquals(msg.getOneofBytes(), undefined); |
234 | 242 |
235 msg.setOneofString('hello'); | 243 msg.setOneofString('hello'); |
236 assertEquals(msg.getOneofUint32(), undefined); | 244 assertEquals(msg.getOneofUint32(), undefined); |
237 assertEquals(msg.getOneofForeignMessage(), undefined); | 245 assertEquals(msg.getOneofForeignMessage(), undefined); |
238 assertEquals(msg.getOneofString(), 'hello'); | 246 assertEquals(msg.getOneofString(), 'hello'); |
239 assertEquals(msg.getOneofBytes(), undefined); | 247 assertEquals(msg.getOneofBytes(), undefined); |
240 | 248 |
241 msg.setOneofBytes('\u00FF\u00FF'); | 249 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
242 assertEquals(msg.getOneofUint32(), undefined); | 250 assertEquals(msg.getOneofUint32(), undefined); |
243 assertEquals(msg.getOneofForeignMessage(), undefined); | 251 assertEquals(msg.getOneofForeignMessage(), undefined); |
244 assertEquals(msg.getOneofString(), undefined); | 252 assertEquals(msg.getOneofString(), undefined); |
245 assertEquals(msg.getOneofBytes(), '\u00FF\u00FF'); | 253 assertEquals(msg.getOneofBytes_asB64(), |
| 254 goog.crypt.base64.encodeString('\u00FF\u00FF')); |
246 }); | 255 }); |
247 | 256 |
248 | 257 |
249 /** | 258 /** |
250 * Test that "default"-valued primitive fields are not emitted on the wire. | 259 * Test that "default"-valued primitive fields are not emitted on the wire. |
251 */ | 260 */ |
252 it('testNoSerializeDefaults', function() { | 261 it('testNoSerializeDefaults', function() { |
253 var msg = new proto.jspb.test.TestProto3(); | 262 var msg = new proto.jspb.test.TestProto3(); |
254 | 263 |
255 // Set each primitive to a non-default value, then back to its default, to | 264 // Set each primitive to a non-default value, then back to its default, to |
256 // ensure that the serialization is actually checking the value and not just | 265 // ensure that the serialization is actually checking the value and not just |
257 // whether it has ever been set. | 266 // whether it has ever been set. |
258 msg.setOptionalInt32(42); | 267 msg.setOptionalInt32(42); |
259 msg.setOptionalInt32(0); | 268 msg.setOptionalInt32(0); |
260 msg.setOptionalDouble(3.14); | 269 msg.setOptionalDouble(3.14); |
261 msg.setOptionalDouble(0.0); | 270 msg.setOptionalDouble(0.0); |
262 msg.setOptionalBool(true); | 271 msg.setOptionalBool(true); |
263 msg.setOptionalBool(false); | 272 msg.setOptionalBool(false); |
264 msg.setOptionalString('hello world'); | 273 msg.setOptionalString('hello world'); |
265 msg.setOptionalString(''); | 274 msg.setOptionalString(''); |
266 msg.setOptionalBytes('\u00FF\u00FF'); | 275 msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
267 msg.setOptionalBytes(''); | 276 msg.setOptionalBytes(''); |
268 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); | 277 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); |
269 msg.setOptionalForeignMessage(null); | 278 msg.setOptionalForeignMessage(null); |
270 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); | 279 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); |
271 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); | 280 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); |
272 msg.setOneofUint32(32); | 281 msg.setOneofUint32(32); |
273 msg.setOneofUint32(null); | 282 msg.setOneofUint32(null); |
274 | 283 |
275 | 284 |
276 var serialized = msg.serializeBinary(); | 285 var serialized = msg.serializeBinary(); |
277 assertEquals(0, serialized.length); | 286 assertEquals(0, serialized.length); |
278 }); | 287 }); |
| 288 |
| 289 /** |
| 290 * Test that base64 string and Uint8Array are interchangeable in bytes fields. |
| 291 */ |
| 292 it('testBytesFieldsInterop', function() { |
| 293 var msg = new proto.jspb.test.TestProto3(); |
| 294 // Set as a base64 string and check all the getters work. |
| 295 msg.setOptionalBytes(BYTES_B64); |
| 296 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); |
| 297 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); |
| 298 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
| 299 |
| 300 // Test binary serialize round trip doesn't break it. |
| 301 msg = proto.jspb.test.TestProto3.deserializeBinary(msg.serializeBinary()); |
| 302 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); |
| 303 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); |
| 304 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
| 305 |
| 306 msg = new proto.jspb.test.TestProto3(); |
| 307 // Set as a Uint8Array and check all the getters work. |
| 308 msg.setOptionalBytes(BYTES); |
| 309 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); |
| 310 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); |
| 311 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
| 312 |
| 313 }); |
279 }); | 314 }); |
OLD | NEW |