/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() /// </summary> private static void AssertReadVarint(byte[] data, ulong value) { CodedInputStream input = CodedInputStream.CreateInstance(data); Assert.AreEqual((uint)value, input.ReadRawVarint32()); input = CodedInputStream.CreateInstance(data); Assert.AreEqual(value, input.ReadRawVarint64()); Assert.IsTrue(input.IsAtEnd); // Try different block sizes. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) { input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual((uint)value, input.ReadRawVarint32()); input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual(value, input.ReadRawVarint64()); Assert.IsTrue(input.IsAtEnd); } // Try reading directly from a MemoryStream. We want to verify that it // doesn't read past the end of the input, so write an extra byte - this // lets us test the position at the end. MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(data, 0, data.Length); memoryStream.WriteByte(0); memoryStream.Position = 0; Assert.AreEqual((uint)value, CodedInputStream.ReadRawVarint32(memoryStream)); Assert.AreEqual(data.Length, memoryStream.Position); }
/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() /// </summary> private static void AssertReadVarint(byte[] data, ulong value) { CodedInputStream input = new CodedInputStream(data); Assert.AreEqual((uint) value, input.ReadRawVarint32()); input = new CodedInputStream(data); Assert.AreEqual(value, input.ReadRawVarint64()); Assert.IsTrue(input.IsAtEnd); // Try different block sizes. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) { input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual((uint) value, input.ReadRawVarint32()); input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual(value, input.ReadRawVarint64()); Assert.IsTrue(input.IsAtEnd); } // Try reading directly from a MemoryStream. We want to verify that it // doesn't read past the end of the input, so write an extra byte - this // lets us test the position at the end. MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(data, 0, data.Length); memoryStream.WriteByte(0); memoryStream.Position = 0; Assert.AreEqual((uint) value, CodedInputStream.ReadRawVarint32(memoryStream)); Assert.AreEqual(data.Length, memoryStream.Position); }
/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() /// </summary> private static void AssertReadVarint(byte[] data, ulong value) { CodedInputStream input = new CodedInputStream(data); Assert.AreEqual((uint)value, input.ReadRawVarint32()); Assert.IsTrue(input.IsAtEnd); input = new CodedInputStream(data); Assert.AreEqual(value, input.ReadRawVarint64()); Assert.IsTrue(input.IsAtEnd); AssertReadFromParseContext(new ReadOnlySequence <byte>(data), (ref ParseContext ctx) => { Assert.AreEqual((uint)value, ctx.ReadUInt32()); }, true); AssertReadFromParseContext(new ReadOnlySequence <byte>(data), (ref ParseContext ctx) => { Assert.AreEqual(value, ctx.ReadUInt64()); }, true); // Try different block sizes. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) { input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual((uint)value, input.ReadRawVarint32()); input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize)); Assert.AreEqual(value, input.ReadRawVarint64()); Assert.IsTrue(input.IsAtEnd); AssertReadFromParseContext(ReadOnlySequenceFactory.CreateWithContent(data, bufferSize), (ref ParseContext ctx) => { Assert.AreEqual((uint)value, ctx.ReadUInt32()); }, true); AssertReadFromParseContext(ReadOnlySequenceFactory.CreateWithContent(data, bufferSize), (ref ParseContext ctx) => { Assert.AreEqual(value, ctx.ReadUInt64()); }, true); } // Try reading directly from a MemoryStream. We want to verify that it // doesn't read past the end of the input, so write an extra byte - this // lets us test the position at the end. MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(data, 0, data.Length); memoryStream.WriteByte(0); memoryStream.Position = 0; Assert.AreEqual((uint)value, CodedInputStream.ReadRawVarint32(memoryStream)); Assert.AreEqual(data.Length, memoryStream.Position); }
/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and /// expects them to fail with an InvalidProtocolBufferException whose /// description matches the given one. /// </summary> private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data) { CodedInputStream input = new CodedInputStream(data); var exception = Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawVarint32()); Assert.AreEqual(expected.Message, exception.Message); input = new CodedInputStream(data); exception = Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawVarint64()); Assert.AreEqual(expected.Message, exception.Message); // Make sure we get the same error when reading directly from a Stream. exception = Assert.Throws <InvalidProtocolBufferException>(() => CodedInputStream.ReadRawVarint32(new MemoryStream(data))); Assert.AreEqual(expected.Message, exception.Message); }
/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and /// expects them to fail with an InvalidProtocolBufferException whose /// description matches the given one. /// </summary> private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data) { CodedInputStream input = new CodedInputStream(data); var exception = Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawVarint32()); Assert.AreEqual(expected.Message, exception.Message); input = new CodedInputStream(data); exception = Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawVarint64()); Assert.AreEqual(expected.Message, exception.Message); AssertReadFromParseContext(new ReadOnlySequence <byte>(data), (ref ParseContext ctx) => { try { ctx.ReadUInt32(); Assert.Fail(); } catch (InvalidProtocolBufferException ex) { Assert.AreEqual(expected.Message, ex.Message); } }, false); AssertReadFromParseContext(new ReadOnlySequence <byte>(data), (ref ParseContext ctx) => { try { ctx.ReadUInt64(); Assert.Fail(); } catch (InvalidProtocolBufferException ex) { Assert.AreEqual(expected.Message, ex.Message); } }, false); // Make sure we get the same error when reading directly from a Stream. exception = Assert.Throws <InvalidProtocolBufferException>(() => CodedInputStream.ReadRawVarint32(new MemoryStream(data))); Assert.AreEqual(expected.Message, exception.Message); }
/// <summary> /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and /// expects them to fail with an InvalidProtocolBufferException whose /// description matches the given one. /// </summary> private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data) { CodedInputStream input = new CodedInputStream(data); var exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint32()); Assert.AreEqual(expected.Message, exception.Message); input = new CodedInputStream(data); exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint64()); Assert.AreEqual(expected.Message, exception.Message); // Make sure we get the same error when reading directly from a Stream. exception = Assert.Throws<InvalidProtocolBufferException>(() => CodedInputStream.ReadRawVarint32(new MemoryStream(data))); Assert.AreEqual(expected.Message, exception.Message); }