OLD | NEW |
| (Empty) |
1 #region Copyright notice and license | |
2 // Protocol Buffers - Google's data interchange format | |
3 // Copyright 2008 Google Inc. All rights reserved. | |
4 // https://developers.google.com/protocol-buffers/ | |
5 // | |
6 // Redistribution and use in source and binary forms, with or without | |
7 // modification, are permitted provided that the following conditions are | |
8 // met: | |
9 // | |
10 // * Redistributions of source code must retain the above copyright | |
11 // notice, this list of conditions and the following disclaimer. | |
12 // * Redistributions in binary form must reproduce the above | |
13 // copyright notice, this list of conditions and the following disclaimer | |
14 // in the documentation and/or other materials provided with the | |
15 // distribution. | |
16 // * Neither the name of Google Inc. nor the names of its | |
17 // contributors may be used to endorse or promote products derived from | |
18 // this software without specific prior written permission. | |
19 // | |
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 #endregion | |
32 | |
33 using System; | |
34 using System.IO; | |
35 using Google.Protobuf.TestProtos; | |
36 using NUnit.Framework; | |
37 | |
38 namespace Google.Protobuf | |
39 { | |
40 public class CodedInputStreamTest | |
41 { | |
42 /// <summary> | |
43 /// Helper to construct a byte array from a bunch of bytes. The inputs
are | |
44 /// actually ints so that I can use hex notation and not get stupid erro
rs | |
45 /// about precision. | |
46 /// </summary> | |
47 private static byte[] Bytes(params int[] bytesAsInts) | |
48 { | |
49 byte[] bytes = new byte[bytesAsInts.Length]; | |
50 for (int i = 0; i < bytesAsInts.Length; i++) | |
51 { | |
52 bytes[i] = (byte) bytesAsInts[i]; | |
53 } | |
54 return bytes; | |
55 } | |
56 | |
57 /// <summary> | |
58 /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() | |
59 /// </summary> | |
60 private static void AssertReadVarint(byte[] data, ulong value) | |
61 { | |
62 CodedInputStream input = new CodedInputStream(data); | |
63 Assert.AreEqual((uint) value, input.ReadRawVarint32()); | |
64 | |
65 input = new CodedInputStream(data); | |
66 Assert.AreEqual(value, input.ReadRawVarint64()); | |
67 Assert.IsTrue(input.IsAtEnd); | |
68 | |
69 // Try different block sizes. | |
70 for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) | |
71 { | |
72 input = new CodedInputStream(new SmallBlockInputStream(data, buf
ferSize)); | |
73 Assert.AreEqual((uint) value, input.ReadRawVarint32()); | |
74 | |
75 input = new CodedInputStream(new SmallBlockInputStream(data, buf
ferSize)); | |
76 Assert.AreEqual(value, input.ReadRawVarint64()); | |
77 Assert.IsTrue(input.IsAtEnd); | |
78 } | |
79 | |
80 // Try reading directly from a MemoryStream. We want to verify that
it | |
81 // doesn't read past the end of the input, so write an extra byte -
this | |
82 // lets us test the position at the end. | |
83 MemoryStream memoryStream = new MemoryStream(); | |
84 memoryStream.Write(data, 0, data.Length); | |
85 memoryStream.WriteByte(0); | |
86 memoryStream.Position = 0; | |
87 Assert.AreEqual((uint) value, CodedInputStream.ReadRawVarint32(memor
yStream)); | |
88 Assert.AreEqual(data.Length, memoryStream.Position); | |
89 } | |
90 | |
91 /// <summary> | |
92 /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64()
and | |
93 /// expects them to fail with an InvalidProtocolBufferException whose | |
94 /// description matches the given one. | |
95 /// </summary> | |
96 private static void AssertReadVarintFailure(InvalidProtocolBufferExcepti
on expected, byte[] data) | |
97 { | |
98 CodedInputStream input = new CodedInputStream(data); | |
99 var exception = Assert.Throws<InvalidProtocolBufferException>(() =>
input.ReadRawVarint32()); | |
100 Assert.AreEqual(expected.Message, exception.Message); | |
101 | |
102 input = new CodedInputStream(data); | |
103 exception = Assert.Throws<InvalidProtocolBufferException>(() => inpu
t.ReadRawVarint64()); | |
104 Assert.AreEqual(expected.Message, exception.Message); | |
105 | |
106 // Make sure we get the same error when reading directly from a Stre
am. | |
107 exception = Assert.Throws<InvalidProtocolBufferException>(() => Code
dInputStream.ReadRawVarint32(new MemoryStream(data))); | |
108 Assert.AreEqual(expected.Message, exception.Message); | |
109 } | |
110 | |
111 [Test] | |
112 public void ReadVarint() | |
113 { | |
114 AssertReadVarint(Bytes(0x00), 0); | |
115 AssertReadVarint(Bytes(0x01), 1); | |
116 AssertReadVarint(Bytes(0x7f), 127); | |
117 // 14882 | |
118 AssertReadVarint(Bytes(0xa2, 0x74), (0x22 << 0) | (0x74 << 7)); | |
119 // 2961488830 | |
120 AssertReadVarint(Bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b), | |
121 (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 <<
21) | | |
122 (0x0bL << 28)); | |
123 | |
124 // 64-bit | |
125 // 7256456126 | |
126 AssertReadVarint(Bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b), | |
127 (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 <<
21) | | |
128 (0x1bL << 28)); | |
129 // 41256202580718336 | |
130 AssertReadVarint(Bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x4
9), | |
131 (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c <<
21) | | |
132 (0x43L << 28) | (0x49L << 35) | (0x24L << 42) | (0x
49L << 49)); | |
133 // 11964378330978735131 | |
134 AssertReadVarint(Bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x8
5, 0xa6, 0x01), | |
135 (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 <<
21) | | |
136 (0x3bUL << 28) | (0x56UL << 35) | (0x00UL << 42) | | |
137 (0x05UL << 49) | (0x26UL << 56) | (0x01UL << 63)); | |
138 | |
139 // Failures | |
140 AssertReadVarintFailure( | |
141 InvalidProtocolBufferException.MalformedVarint(), | |
142 Bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80
, | |
143 0x00)); | |
144 AssertReadVarintFailure( | |
145 InvalidProtocolBufferException.TruncatedMessage(), | |
146 Bytes(0x80)); | |
147 } | |
148 | |
149 /// <summary> | |
150 /// Parses the given bytes using ReadRawLittleEndian32() and checks | |
151 /// that the result matches the given value. | |
152 /// </summary> | |
153 private static void AssertReadLittleEndian32(byte[] data, uint value) | |
154 { | |
155 CodedInputStream input = new CodedInputStream(data); | |
156 Assert.AreEqual(value, input.ReadRawLittleEndian32()); | |
157 Assert.IsTrue(input.IsAtEnd); | |
158 | |
159 // Try different block sizes. | |
160 for (int blockSize = 1; blockSize <= 16; blockSize *= 2) | |
161 { | |
162 input = new CodedInputStream( | |
163 new SmallBlockInputStream(data, blockSize)); | |
164 Assert.AreEqual(value, input.ReadRawLittleEndian32()); | |
165 Assert.IsTrue(input.IsAtEnd); | |
166 } | |
167 } | |
168 | |
169 /// <summary> | |
170 /// Parses the given bytes using ReadRawLittleEndian64() and checks | |
171 /// that the result matches the given value. | |
172 /// </summary> | |
173 private static void AssertReadLittleEndian64(byte[] data, ulong value) | |
174 { | |
175 CodedInputStream input = new CodedInputStream(data); | |
176 Assert.AreEqual(value, input.ReadRawLittleEndian64()); | |
177 Assert.IsTrue(input.IsAtEnd); | |
178 | |
179 // Try different block sizes. | |
180 for (int blockSize = 1; blockSize <= 16; blockSize *= 2) | |
181 { | |
182 input = new CodedInputStream( | |
183 new SmallBlockInputStream(data, blockSize)); | |
184 Assert.AreEqual(value, input.ReadRawLittleEndian64()); | |
185 Assert.IsTrue(input.IsAtEnd); | |
186 } | |
187 } | |
188 | |
189 [Test] | |
190 public void ReadLittleEndian() | |
191 { | |
192 AssertReadLittleEndian32(Bytes(0x78, 0x56, 0x34, 0x12), 0x12345678); | |
193 AssertReadLittleEndian32(Bytes(0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef0); | |
194 | |
195 AssertReadLittleEndian64(Bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0
x34, 0x12), | |
196 0x123456789abcdef0L); | |
197 AssertReadLittleEndian64( | |
198 Bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef
012345678UL); | |
199 } | |
200 | |
201 [Test] | |
202 public void DecodeZigZag32() | |
203 { | |
204 Assert.AreEqual(0, CodedInputStream.DecodeZigZag32(0)); | |
205 Assert.AreEqual(-1, CodedInputStream.DecodeZigZag32(1)); | |
206 Assert.AreEqual(1, CodedInputStream.DecodeZigZag32(2)); | |
207 Assert.AreEqual(-2, CodedInputStream.DecodeZigZag32(3)); | |
208 Assert.AreEqual(0x3FFFFFFF, CodedInputStream.DecodeZigZag32(0x7FFFFF
FE)); | |
209 Assert.AreEqual(unchecked((int) 0xC0000000), CodedInputStream.Decode
ZigZag32(0x7FFFFFFF)); | |
210 Assert.AreEqual(0x7FFFFFFF, CodedInputStream.DecodeZigZag32(0xFFFFFF
FE)); | |
211 Assert.AreEqual(unchecked((int) 0x80000000), CodedInputStream.Decode
ZigZag32(0xFFFFFFFF)); | |
212 } | |
213 | |
214 [Test] | |
215 public void DecodeZigZag64() | |
216 { | |
217 Assert.AreEqual(0, CodedInputStream.DecodeZigZag64(0)); | |
218 Assert.AreEqual(-1, CodedInputStream.DecodeZigZag64(1)); | |
219 Assert.AreEqual(1, CodedInputStream.DecodeZigZag64(2)); | |
220 Assert.AreEqual(-2, CodedInputStream.DecodeZigZag64(3)); | |
221 Assert.AreEqual(0x000000003FFFFFFFL, CodedInputStream.DecodeZigZag64
(0x000000007FFFFFFEL)); | |
222 Assert.AreEqual(unchecked((long) 0xFFFFFFFFC0000000L), CodedInputStr
eam.DecodeZigZag64(0x000000007FFFFFFFL)); | |
223 Assert.AreEqual(0x000000007FFFFFFFL, CodedInputStream.DecodeZigZag64
(0x00000000FFFFFFFEL)); | |
224 Assert.AreEqual(unchecked((long) 0xFFFFFFFF80000000L), CodedInputStr
eam.DecodeZigZag64(0x00000000FFFFFFFFL)); | |
225 Assert.AreEqual(0x7FFFFFFFFFFFFFFFL, CodedInputStream.DecodeZigZag64
(0xFFFFFFFFFFFFFFFEL)); | |
226 Assert.AreEqual(unchecked((long) 0x8000000000000000L), CodedInputStr
eam.DecodeZigZag64(0xFFFFFFFFFFFFFFFFL)); | |
227 } | |
228 | |
229 [Test] | |
230 public void ReadWholeMessage_VaryingBlockSizes() | |
231 { | |
232 TestAllTypes message = SampleMessages.CreateFullTestAllTypes(); | |
233 | |
234 byte[] rawBytes = message.ToByteArray(); | |
235 Assert.AreEqual(rawBytes.Length, message.CalculateSize()); | |
236 TestAllTypes message2 = TestAllTypes.Parser.ParseFrom(rawBytes); | |
237 Assert.AreEqual(message, message2); | |
238 | |
239 // Try different block sizes. | |
240 for (int blockSize = 1; blockSize < 256; blockSize *= 2) | |
241 { | |
242 message2 = TestAllTypes.Parser.ParseFrom(new SmallBlockInputStre
am(rawBytes, blockSize)); | |
243 Assert.AreEqual(message, message2); | |
244 } | |
245 } | |
246 | |
247 [Test] | |
248 public void ReadHugeBlob() | |
249 { | |
250 // Allocate and initialize a 1MB blob. | |
251 byte[] blob = new byte[1 << 20]; | |
252 for (int i = 0; i < blob.Length; i++) | |
253 { | |
254 blob[i] = (byte) i; | |
255 } | |
256 | |
257 // Make a message containing it. | |
258 var message = new TestAllTypes { SingleBytes = ByteString.CopyFrom(b
lob) }; | |
259 | |
260 // Serialize and parse it. Make sure to parse from an InputStream,
not | |
261 // directly from a ByteString, so that CodedInputStream uses buffere
d | |
262 // reading. | |
263 TestAllTypes message2 = TestAllTypes.Parser.ParseFrom(message.ToByte
String()); | |
264 | |
265 Assert.AreEqual(message, message2); | |
266 } | |
267 | |
268 [Test] | |
269 public void ReadMaliciouslyLargeBlob() | |
270 { | |
271 MemoryStream ms = new MemoryStream(); | |
272 CodedOutputStream output = new CodedOutputStream(ms); | |
273 | |
274 uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited
); | |
275 output.WriteRawVarint32(tag); | |
276 output.WriteRawVarint32(0x7FFFFFFF); | |
277 output.WriteRawBytes(new byte[32]); // Pad with a few random bytes. | |
278 output.Flush(); | |
279 ms.Position = 0; | |
280 | |
281 CodedInputStream input = new CodedInputStream(ms); | |
282 Assert.AreEqual(tag, input.ReadTag()); | |
283 | |
284 Assert.Throws<InvalidProtocolBufferException>(() => input.ReadBytes(
)); | |
285 } | |
286 | |
287 private static TestRecursiveMessage MakeRecursiveMessage(int depth) | |
288 { | |
289 if (depth == 0) | |
290 { | |
291 return new TestRecursiveMessage { I = 5 }; | |
292 } | |
293 else | |
294 { | |
295 return new TestRecursiveMessage { A = MakeRecursiveMessage(depth
- 1) }; | |
296 } | |
297 } | |
298 | |
299 private static void AssertMessageDepth(TestRecursiveMessage message, int
depth) | |
300 { | |
301 if (depth == 0) | |
302 { | |
303 Assert.IsNull(message.A); | |
304 Assert.AreEqual(5, message.I); | |
305 } | |
306 else | |
307 { | |
308 Assert.IsNotNull(message.A); | |
309 AssertMessageDepth(message.A, depth - 1); | |
310 } | |
311 } | |
312 | |
313 [Test] | |
314 public void MaliciousRecursion() | |
315 { | |
316 ByteString data64 = MakeRecursiveMessage(64).ToByteString(); | |
317 ByteString data65 = MakeRecursiveMessage(65).ToByteString(); | |
318 | |
319 AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(data64), 64
); | |
320 | |
321 Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMes
sage.Parser.ParseFrom(data65)); | |
322 | |
323 CodedInputStream input = CodedInputStream.CreateWithLimits(new Memor
yStream(data64.ToByteArray()), 1000000, 63); | |
324 Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMes
sage.Parser.ParseFrom(input)); | |
325 } | |
326 | |
327 [Test] | |
328 public void SizeLimit() | |
329 { | |
330 // Have to use a Stream rather than ByteString.CreateCodedInput as S
izeLimit doesn't | |
331 // apply to the latter case. | |
332 MemoryStream ms = new MemoryStream(SampleMessages.CreateFullTestAllT
ypes().ToByteArray()); | |
333 CodedInputStream input = CodedInputStream.CreateWithLimits(ms, 16, 1
00); | |
334 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseFrom(input)); | |
335 } | |
336 | |
337 /// <summary> | |
338 /// Tests that if we read an string that contains invalid UTF-8, no exce
ption | |
339 /// is thrown. Instead, the invalid bytes are replaced with the Unicode | |
340 /// "replacement character" U+FFFD. | |
341 /// </summary> | |
342 [Test] | |
343 public void ReadInvalidUtf8() | |
344 { | |
345 MemoryStream ms = new MemoryStream(); | |
346 CodedOutputStream output = new CodedOutputStream(ms); | |
347 | |
348 uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited
); | |
349 output.WriteRawVarint32(tag); | |
350 output.WriteRawVarint32(1); | |
351 output.WriteRawBytes(new byte[] {0x80}); | |
352 output.Flush(); | |
353 ms.Position = 0; | |
354 | |
355 CodedInputStream input = new CodedInputStream(ms); | |
356 | |
357 Assert.AreEqual(tag, input.ReadTag()); | |
358 string text = input.ReadString(); | |
359 Assert.AreEqual('\ufffd', text[0]); | |
360 } | |
361 | |
362 /// <summary> | |
363 /// A stream which limits the number of bytes it reads at a time. | |
364 /// We use this to make sure that CodedInputStream doesn't screw up when | |
365 /// reading in small blocks. | |
366 /// </summary> | |
367 private sealed class SmallBlockInputStream : MemoryStream | |
368 { | |
369 private readonly int blockSize; | |
370 | |
371 public SmallBlockInputStream(byte[] data, int blockSize) | |
372 : base(data) | |
373 { | |
374 this.blockSize = blockSize; | |
375 } | |
376 | |
377 public override int Read(byte[] buffer, int offset, int count) | |
378 { | |
379 return base.Read(buffer, offset, Math.Min(count, blockSize)); | |
380 } | |
381 } | |
382 | |
383 [Test] | |
384 public void TestNegativeEnum() | |
385 { | |
386 byte[] bytes = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF
F, 0x01 }; | |
387 CodedInputStream input = new CodedInputStream(bytes); | |
388 Assert.AreEqual((int)SampleEnum.NegativeValue, input.ReadEnum()); | |
389 Assert.IsTrue(input.IsAtEnd); | |
390 } | |
391 | |
392 //Issue 71: CodedInputStream.ReadBytes go to slow path unnecessarily | |
393 [Test] | |
394 public void TestSlowPathAvoidance() | |
395 { | |
396 using (var ms = new MemoryStream()) | |
397 { | |
398 CodedOutputStream output = new CodedOutputStream(ms); | |
399 output.WriteTag(1, WireFormat.WireType.LengthDelimited); | |
400 output.WriteBytes(ByteString.CopyFrom(new byte[100])); | |
401 output.WriteTag(2, WireFormat.WireType.LengthDelimited); | |
402 output.WriteBytes(ByteString.CopyFrom(new byte[100])); | |
403 output.Flush(); | |
404 | |
405 ms.Position = 0; | |
406 CodedInputStream input = new CodedInputStream(ms, new byte[ms.Le
ngth / 2], 0, 0); | |
407 | |
408 uint tag = input.ReadTag(); | |
409 Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag)); | |
410 Assert.AreEqual(100, input.ReadBytes().Length); | |
411 | |
412 tag = input.ReadTag(); | |
413 Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag)); | |
414 Assert.AreEqual(100, input.ReadBytes().Length); | |
415 } | |
416 } | |
417 | |
418 [Test] | |
419 public void Tag0Throws() | |
420 { | |
421 var input = new CodedInputStream(new byte[] { 0 }); | |
422 Assert.Throws<InvalidProtocolBufferException>(() => input.ReadTag())
; | |
423 } | |
424 | |
425 [Test] | |
426 public void SkipGroup() | |
427 { | |
428 // Create an output stream with a group in: | |
429 // Field 1: string "field 1" | |
430 // Field 2: group containing: | |
431 // Field 1: fixed int32 value 100 | |
432 // Field 2: string "ignore me" | |
433 // Field 3: nested group containing | |
434 // Field 1: fixed int64 value 1000 | |
435 // Field 3: string "field 3" | |
436 var stream = new MemoryStream(); | |
437 var output = new CodedOutputStream(stream); | |
438 output.WriteTag(1, WireFormat.WireType.LengthDelimited); | |
439 output.WriteString("field 1"); | |
440 | |
441 // The outer group... | |
442 output.WriteTag(2, WireFormat.WireType.StartGroup); | |
443 output.WriteTag(1, WireFormat.WireType.Fixed32); | |
444 output.WriteFixed32(100); | |
445 output.WriteTag(2, WireFormat.WireType.LengthDelimited); | |
446 output.WriteString("ignore me"); | |
447 // The nested group... | |
448 output.WriteTag(3, WireFormat.WireType.StartGroup); | |
449 output.WriteTag(1, WireFormat.WireType.Fixed64); | |
450 output.WriteFixed64(1000); | |
451 // Note: Not sure the field number is relevant for end group... | |
452 output.WriteTag(3, WireFormat.WireType.EndGroup); | |
453 | |
454 // End the outer group | |
455 output.WriteTag(2, WireFormat.WireType.EndGroup); | |
456 | |
457 output.WriteTag(3, WireFormat.WireType.LengthDelimited); | |
458 output.WriteString("field 3"); | |
459 output.Flush(); | |
460 stream.Position = 0; | |
461 | |
462 // Now act like a generated client | |
463 var input = new CodedInputStream(stream); | |
464 Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDeli
mited), input.ReadTag()); | |
465 Assert.AreEqual("field 1", input.ReadString()); | |
466 Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup
), input.ReadTag()); | |
467 input.SkipLastField(); // Should consume the whole group, including
the nested one. | |
468 Assert.AreEqual(WireFormat.MakeTag(3, WireFormat.WireType.LengthDeli
mited), input.ReadTag()); | |
469 Assert.AreEqual("field 3", input.ReadString()); | |
470 } | |
471 | |
472 [Test] | |
473 public void EndOfStreamReachedWhileSkippingGroup() | |
474 { | |
475 var stream = new MemoryStream(); | |
476 var output = new CodedOutputStream(stream); | |
477 output.WriteTag(1, WireFormat.WireType.StartGroup); | |
478 output.WriteTag(2, WireFormat.WireType.StartGroup); | |
479 output.WriteTag(2, WireFormat.WireType.EndGroup); | |
480 | |
481 output.Flush(); | |
482 stream.Position = 0; | |
483 | |
484 // Now act like a generated client | |
485 var input = new CodedInputStream(stream); | |
486 input.ReadTag(); | |
487 Assert.Throws<InvalidProtocolBufferException>(() => input.SkipLastFi
eld()); | |
488 } | |
489 | |
490 [Test] | |
491 public void RecursionLimitAppliedWhileSkippingGroup() | |
492 { | |
493 var stream = new MemoryStream(); | |
494 var output = new CodedOutputStream(stream); | |
495 for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) | |
496 { | |
497 output.WriteTag(1, WireFormat.WireType.StartGroup); | |
498 } | |
499 for (int i = 0; i < CodedInputStream.DefaultRecursionLimit + 1; i++) | |
500 { | |
501 output.WriteTag(1, WireFormat.WireType.EndGroup); | |
502 } | |
503 output.Flush(); | |
504 stream.Position = 0; | |
505 | |
506 // Now act like a generated client | |
507 var input = new CodedInputStream(stream); | |
508 Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.StartGroup
), input.ReadTag()); | |
509 Assert.Throws<InvalidProtocolBufferException>(() => input.SkipLastFi
eld()); | |
510 } | |
511 | |
512 [Test] | |
513 public void Construction_Invalid() | |
514 { | |
515 Assert.Throws<ArgumentNullException>(() => new CodedInputStream((byt
e[]) null)); | |
516 Assert.Throws<ArgumentNullException>(() => new CodedInputStream(null
, 0, 0)); | |
517 Assert.Throws<ArgumentNullException>(() => new CodedInputStream((Str
eam) null)); | |
518 Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStrea
m(new byte[10], 100, 0)); | |
519 Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStrea
m(new byte[10], 5, 10)); | |
520 } | |
521 | |
522 [Test] | |
523 public void CreateWithLimits_InvalidLimits() | |
524 { | |
525 var stream = new MemoryStream(); | |
526 Assert.Throws<ArgumentOutOfRangeException>(() => CodedInputStream.Cr
eateWithLimits(stream, 0, 1)); | |
527 Assert.Throws<ArgumentOutOfRangeException>(() => CodedInputStream.Cr
eateWithLimits(stream, 1, 0)); | |
528 } | |
529 } | |
530 } | |
OLD | NEW |