Ejemplo n.º 1
0
                public void ShouldUnpackTinyStruct()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(0xB2);

                    var u = new PackStream.Unpacker(mockInput.Object, null);

                    u.UnpackStructHeader().Should().Be(2);
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                }
Ejemplo n.º 2
0
                public void ShouldUnpackBooleanFalseSuccessfully()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.FALSE);

                    var u = new PackStream.Unpacker(mockInput.Object, null);

                    u.UnpackBoolean().Should().BeFalse();
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                }
Ejemplo n.º 3
0
                public void ShouldUnpackTinyStringAsEmptyString()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.TINY_STRING);

                    var u = new PackStream.Unpacker(mockInput.Object, null);

                    u.UnpackString().Should().BeEmpty(); //.Equals(String.Empty);
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                }
Ejemplo n.º 4
0
                public void ShouldCallReadByteOnce()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(0xFF);

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    u.UnpackStructSignature().Should().Be(0xFF);
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                }
Ejemplo n.º 5
0
                internal void ShouldPeekTypeCorrectly(byte marker, PackStream.PackType expected)
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.PeekByte()).Returns(marker);

                    var u = new PackStream.Unpacker(mockInput.Object, null);

                    u.PeekNextType().Should().Be(expected);
                    mockInput.Verify(x => x.PeekByte(), Times.Once);
                }
Ejemplo n.º 6
0
                public void ShouldUnpackNullSuccessfully()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.NULL);

                    var u = new PackStream.Unpacker(mockInput.Object, null);

                    u.UnpackNull().Should().BeNull();
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                }
Ejemplo n.º 7
0
                public void ShouldThrowExceptionIfMarkerByteNotTrueOrFalse()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.BYTES_16);

                    var unpacker = new PackStream.Unpacker(mockInput.Object, null);

                    var ex = Xunit.Record.Exception(() => unpacker.UnpackBoolean());

                    ex.Should().BeOfType <ArgumentOutOfRangeException>();
                }
Ejemplo n.º 8
0
                public void ShouldUnpackStruct8()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte())
                    .Returns(new Queue <byte>(new[] { PackStream.STRUCT_8, (byte)1 }).Dequeue);

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    u.UnpackStructHeader().Should().Be(1);
                    mockInput.Verify(x => x.ReadByte(), Times.Exactly(2));
                }
Ejemplo n.º 9
0
                public void ShouldThrowExceptionIfMarkerByteNotNull()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.BYTES_16);

                    var unpacker = new PackStream.Unpacker(mockInput.Object);

                    var ex = Xunit.Record.Exception(() => unpacker.UnpackNull());

                    ex.Should().BeOfType <ProtocolException>();
                }
Ejemplo n.º 10
0
                public void ShouldUnpackList8()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte())
                    .Returns(new Queue <byte>(new[] { PackStream.LIST_8, (byte)1 }).Dequeue);

                    var u = new PackStream.Unpacker(mockInput.Object);

                    u.UnpackListHeader().Should().Be(1);
                    mockInput.Verify(x => x.ReadByte(), Times.Exactly(2));
                }
Ejemplo n.º 11
0
                public void ShouldUnpackList16()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.LIST_16);
                    mockInput.Setup(x => x.ReadShort()).Returns(1);

                    var u = new PackStream.Unpacker(mockInput.Object);

                    u.UnpackListHeader().Should().Be(1);
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadShort(), Times.Once);
                }
Ejemplo n.º 12
0
                public void ShouldThrowExceptionIfMarkerByteNotStruct()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.FALSE);

                    var u = new PackStream.Unpacker(mockInput.Object);

                    var ex = Xunit.Record.Exception(() => u.UnpackStructHeader());

                    ex.Should().BeOfType <ProtocolException>();
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                }
Ejemplo n.º 13
0
                public void ShouldThrowExceptionIfMarkerByteUnDefined()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.PeekByte()).Returns(PackStream.RESERVED_C4);

                    var u = new PackStream.Unpacker(mockInput.Object);

                    var ex = Xunit.Record.Exception(() => u.PeekNextType());

                    ex.Should().BeOfType <ProtocolException>();
                    mockInput.Verify(x => x.PeekByte(), Times.Once);
                }
Ejemplo n.º 14
0
                public void ShouldThrowExceptionIfMarkerByteUnDefined()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.PeekByte()).Returns(PackStream.RESERVED_C4);

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    var ex = Xunit.Record.Exception(() => u.PeekNextType());

                    ex.Should().BeOfType <ArgumentOutOfRangeException>();
                    mockInput.Verify(x => x.PeekByte(), Times.Once);
                }
Ejemplo n.º 15
0
                public void ShouldUnpackStruct16()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.STRUCT_16);
                    mockInput.Setup(x => x.ReadShort()).Returns(1);

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    u.UnpackStructHeader().Should().Be(1);
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadShort(), Times.Once);
                }
Ejemplo n.º 16
0
                public void ShouldThrowExceptionIfMarkerByteNotStruct()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.FALSE);

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    var ex = Xunit.Record.Exception(() => u.UnpackStructHeader());

                    ex.Should().BeOfType <ArgumentOutOfRangeException>();
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                }
Ejemplo n.º 17
0
                public void ShouldUnpackList32()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.LIST_32);
                    mockInput.Setup(x => x.ReadInt()).Returns(-1);

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    u.UnpackListHeader().Should().Be(uint.MaxValue);
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadInt(), Times.Once);
                }
Ejemplo n.º 18
0
                public void ShouldUnpackMap32()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.MAP_32);
                    mockInput.Setup(x => x.ReadInt()).Returns(-1);

                    var u = new PackStream.Unpacker(mockInput.Object);

                    u.UnpackMapHeader().Should().Be(uint.MaxValue);
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadInt(), Times.Once);
                }
Ejemplo n.º 19
0
                public void ShouldUnpackStringLessThan16Chars()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(0x81);
                    mockInput.Setup(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()))
                    .Callback <byte[], int, int?>((buffer, offset, size) => { buffer[0] = 0x61; });

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    u.UnpackString().Should().Be("a");
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()), Times.Once);
                }
Ejemplo n.º 20
0
                public void ShouldUnpackLongAsLong()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.INT_64);
                    long expected = 1024;

                    mockInput.Setup(x => x.ReadLong()).Returns(expected);

                    var u = new PackStream.Unpacker(mockInput.Object, null);

                    Assert.Equal(expected, u.UnpackLong());
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadLong(), Times.Once);
                }
Ejemplo n.º 21
0
                public void ShouldThrowExceptionWhenUnpackBytes32ReturnsBytesSizeLonggerThanIntMax()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.BYTES_32);
                    mockInput.Setup(x => x.ReadInt()).Returns(-1);

                    var u = new PackStream.Unpacker(mockInput.Object);

                    var ex = Xunit.Record.Exception(() => u.UnpackBytes());

                    ex.Should().BeOfType <ProtocolException>();
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadInt(), Times.Once);
                }
Ejemplo n.º 22
0
                public void ShouldThrowExceptionWhenUnpackString32ReturnsStringSizeLonggerThanIntMax()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.STRING_32);
                    mockInput.Setup(x => x.ReadInt()).Returns(-1);

                    var u = new PackStream.Unpacker(mockInput.Object);

                    var ex = Xunit.Record.Exception(() => u.UnpackString());

                    ex.Should().BeOfType <ArgumentOutOfRangeException>();
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadInt(), Times.Once);
                }
Ejemplo n.º 23
0
                [InlineData(0x7F, 127)] // 7F to FF
                public void ShouldUnpackLongAsTinyByte(byte data, sbyte expected)
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(data);

                    var u = new PackStream.Unpacker(mockInput.Object, null);

                    sbyte real = (sbyte)u.UnpackLong();

                    real.Should().Be(expected);

                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadSByte(), Times.Never);
                }
Ejemplo n.º 24
0
                public void ShouldUnpackDoubleCorrectly()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.FLOAT_64);
                    double expected = 1.12;

                    mockInput.Setup(x => x.ReadDouble()).Returns(expected);

                    var u = new PackStream.Unpacker(mockInput.Object, null);

                    Assert.Equal(expected, u.UnpackDouble());
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadDouble(), Times.Once);
                }
Ejemplo n.º 25
0
                public void ShouldUnpackString8()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte())
                    .Returns(new Queue <byte>(new[] { PackStream.STRING_8, (byte)1 }).Dequeue);
                    mockInput.Setup(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()))
                    .Callback <byte[], int, int?>((buffer, offset, size) => { buffer[0] = 0x61; });

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    u.UnpackString().Should().Be("a");
                    mockInput.Verify(x => x.ReadByte(), Times.Exactly(2));
                    mockInput.Verify(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()), Times.Once);
                }
Ejemplo n.º 26
0
                public void ShouldThrowExceptionWhenUnpackBytes32ReturnsBytesSizeLonggerThanIntMax()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.BYTES_32);
                    mockInput.Setup(x => x.ReadInt()).Returns(-1);

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    var ex = Xunit.Record.Exception(() => u.UnpackBytes());

                    ex.Should().BeOfType <ArgumentOutOfRangeException>();
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadInt(), Times.Once);
                }
Ejemplo n.º 27
0
                public void ShouldUnpackString32()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.STRING_32);
                    mockInput.Setup(x => x.ReadInt()).Returns(1);
                    mockInput.Setup(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()))
                    .Callback <byte[], int, int?>((buffer, offset, size) => { buffer[0] = 0x61; });

                    var u = new PackStream.Unpacker(mockInput.Object);

                    u.UnpackString().Should().Be("a");
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadInt(), Times.Once);
                    mockInput.Verify(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()), Times.Once);
                }
Ejemplo n.º 28
0
                public void ShouldUnpackLongAsSignedByte()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.INT_8);
                    sbyte expected = 1;

                    mockInput.Setup(x => x.ReadSByte()).Returns(expected);

                    var u = new PackStream.Unpacker(mockInput.Object, null);

                    sbyte real = (sbyte)u.UnpackLong();

                    Assert.Equal(expected, real);
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadSByte(), Times.Once);
                }
Ejemplo n.º 29
0
                public void ShouldUnpackBytes8()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte())
                    .Returns(new Queue <byte>(new[] { PackStream.BYTES_8, (byte)1 }).Dequeue);
                    mockInput.Setup(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()))
                    .Callback <byte[], int, int?>((buffer, offset, size) => { buffer[0] = 0x61; });

                    var u = new PackStream.Unpacker(mockInput.Object);

                    var actual = u.UnpackBytes();

                    actual.Length.Should().Be(1);
                    actual.Should().Contain(0x61);
                    mockInput.Verify(x => x.ReadByte(), Times.Exactly(2));
                    mockInput.Verify(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()), Times.Once);
                }
Ejemplo n.º 30
0
                public void ShouldUnpackBytes32()
                {
                    var mockInput = new Mock <IInputStream>();

                    mockInput.Setup(x => x.ReadByte()).Returns(PackStream.BYTES_32);
                    mockInput.Setup(x => x.ReadInt()).Returns(1);
                    mockInput.Setup(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()))
                    .Callback <byte[], int, int?>((buffer, offset, size) => { buffer[0] = 0x61; });

                    var u = new PackStream.Unpacker(mockInput.Object, new BigEndianTargetBitConverter());

                    var actual = u.UnpackBytes();

                    actual.Length.Should().Be(1);
                    actual.Should().Contain(0x61);
                    mockInput.Verify(x => x.ReadByte(), Times.Once);
                    mockInput.Verify(x => x.ReadInt(), Times.Once);
                    mockInput.Verify(x => x.ReadBytes(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int?>()), Times.Once);
                }