Files
DotNetty/test/DotNetty.Codecs.Tests/Protobuf/ProtobufVarint32FrameDecoderTest.cs
Johnny Z 6d1a58df8e Initial Protobuf 2/3 Codec Implementation ( #118) (#163)
Motivation:
Ported/Re-implemented Protobuf codec support into DotNetty

Modifications:
- Added Protobuf 2/3 Codec project
- Added Protobuf 2/3 Codec unit test project.
- Extra: Ported MessageToByteEncoder to DotNetty.Codec.

Result:
Protobuf 2/3 codec supported by DotNetty.
2016-11-16 14:00:06 -08:00

88 lines
3.1 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace DotNetty.Codecs.Tests.Protobuf
{
using System;
using System.Linq;
using DotNetty.Buffers;
using DotNetty.Codecs.Protobuf;
using DotNetty.Transport.Channels.Embedded;
using Xunit;
public sealed class ProtobufVarint32FrameDecoderTest
{
[Fact]
public void TinyDecode()
{
byte[] bytes = { 4, 1, 1, 1, 1 };
IByteBuffer content = Unpooled.WrappedBuffer(bytes, 0, 1);
var channel = new EmbeddedChannel(new ProtobufVarint32FrameDecoder());
Assert.False(channel.WriteInbound(content));
var written = channel.ReadInbound<IByteBuffer>();
Assert.Null(written);
content = Unpooled.WrappedBuffer(bytes, 1, 2);
Assert.False(channel.WriteInbound(content));
written = channel.ReadInbound<IByteBuffer>();
Assert.Null(written);
content = Unpooled.WrappedBuffer(bytes, 3, bytes.Length - 3);
Assert.True(channel.WriteInbound(content));
written = channel.ReadInbound<IByteBuffer>();
Assert.NotNull(written);
byte[] output = TestUtil.GetReadableBytes(written);
var expected = new byte[] { 1, 1, 1, 1 };
Assert.Equal(expected.Length, output.Length);
Assert.True(output.SequenceEqual(expected));
written.Release();
Assert.False(channel.Finish());
}
[Fact]
public void RegularDecode()
{
var bytes = new byte[2048];
for (int i = 2; i < 2048; i++)
{
bytes[i] = 1;
}
bytes[0] = -2 + 256;
bytes[1] = 15;
var channel = new EmbeddedChannel(new ProtobufVarint32FrameDecoder());
IByteBuffer content = Unpooled.WrappedBuffer(bytes, 0, 1);
Assert.False(channel.WriteInbound(content));
var written = channel.ReadInbound<IByteBuffer>();
Assert.Null(written);
content = Unpooled.WrappedBuffer(bytes, 1, 127);
Assert.False(channel.WriteInbound(content));
written = channel.ReadInbound<IByteBuffer>();
Assert.Null(written);
content = Unpooled.WrappedBuffer(bytes, 127, 600);
Assert.False(channel.WriteInbound(content));
written = channel.ReadInbound<IByteBuffer>();
Assert.Null(written);
content = Unpooled.WrappedBuffer(bytes, 727, bytes.Length - 727);
Assert.True(channel.WriteInbound(content));
written = channel.ReadInbound<IByteBuffer>();
byte[] output = TestUtil.GetReadableBytes(written);
var expected = new byte[bytes.Length - 2];
Buffer.BlockCopy(bytes, 2, expected, 0, bytes.Length - 2);
Assert.Equal(expected.Length, output.Length);
Assert.True(output.SequenceEqual(expected));
written.Release();
Assert.False(channel.Finish());
}
}
}